c# - Creating a List<> of a Subset of Keys and Strings in a .RESX file -
i have .resx file contains strings website. i'd create list<string> of subset of these strings without having use add() each new string this:
list<string> listofresourcestrings= new list<string>(); listofresourcestrings.add(resources.website_string1); listofresourcestrings.add(resources.website_string2); listofresourcestrings.add(resources.website_string3); listofresourcestrings.add(resources.website_string4); listofresourcestrings.add(resources.website_string5); listofresourcestrings.add(resources.website_stringn); i use...
system.resources.resourceset listofresourcestrings = resources.resourcemanager.getresourceset(system.threading.thread.currentthread.currentculture, true, true); ...but returns resourceset , contains all strings. and, there doesn't seem easy way find subset of strings.
thank help,
aaron
resource data stored in static properties, reflection can used selectively extract property values based on property name.
using system; using system.collections.generic; using system.reflection; using system.text.regularexpressions; namespace consoleapplication9 { public class program { public static void main(string[] args) { var strings = getstringpropertyvaluesfromtype(typeof(properties.resources), @"^website_.*"); foreach (var s in strings) console.writeline(s); } public static list<string> getstringpropertyvaluesfromtype(type type, string propertynamemask) { var result = new list<string>(); var propertyinfos = type.getproperties(bindingflags.public | bindingflags.nonpublic | bindingflags.static); var regex = new regex(propertynamemask, regexoptions.ignorecase | regexoptions.singleline); foreach (var propertyinfo in propertyinfos) { if (propertyinfo.canread && (propertyinfo.propertytype == typeof(string)) && regex.ismatch(propertyinfo.name)) { result.add(propertyinfo.getvalue(type, null).tostring()); } } return result; } } }
Comments
Post a Comment