c# - How to use a Variable Name which was Obtained at Run-Time -
all, provide on-the-fly mechanism debugging application in different languages using required resource string (in foreign language) display english equivalent @ run-time should user require it. done using
public static string getmessage(string messagekey) { cultureinfo culture = thread.currentthread.currentculture; if (!culture.displayname.contains("english")) { string filename = "messagestrings.resx"; string appdir = path.getdirectoryname(application.executablepath); filename = path.combine(appdir, filename); if (file.exists(filename)) { // english error message. using (resxresourcereader resxreader = new resxresourcereader(filename)) { foreach (dictionaryentry e in resxreader) if (e.key.tostring().comparenocase(messagekey) == 0) return e.value.tostring(); } } } return null; }
where getname
defined
public static string getname<t>(expression<func<t>> expression) { return ((memberexpression)expression.body).member.name; }
i display localised messages in application
utils.errmsg(messagestrings.somemessage);
or
utils.errmsg(string.format(messagestrings.somemessage, param1, param2));
now can display relevent english message app running in different culture using
utils.errmsg(utils.getmessage( utils.getname(() => messagestrings.errcellallocstatzerototal)) ?? messagestrings.errcellallocstatzerototal);
i want avoid having use lambda expression in call getname
, use of null
getmessage
, using ??
, how can achieve [if @ possible]?
thanks time.
i not understand code, if want access properties of object dynamically, try (you have replace [object] , "propertyname" specific values):
// property object propertyinfo property = [object].gettype().getproperty("propertyname"); // value int value = (int)property.getvalue([object], null);
Comments
Post a Comment