c# - How to access a dynamic object's property dynamically (by variable)? -
i have class named configuration
which inherits dynamicobject
. it dictionary<string, object>
. in constructor, reads text file , loads values splitted =
, can create dynamic configuration objects this:
dynamic configuration = new configuration("some_file.ini");
here full class:
public sealed class configuration : dynamicobject { private string path { get; set; } private dictionary<string, object> dictionary { get; set; } public configuration(string filename) { this.path = filename; this.dictionary = new dictionary<string, object>(); this.populate(); } ~configuration() { if (this.dictionary != null) { this.dictionary.clear(); } } private void populate() { using (streamreader reader = new streamreader(this.path)) { string line; string currentsection = string.empty; while ((line = reader.readline()) != null) { if (string.isnullorwhitespace(line)) { continue; } if (line.startswith("[") && line.endswith("]")) { currentsection = line.trim('[', ']'); } else if (line.contains("=")) { this.dictionary.add(string.format("{0}{1}{2}", currentsection, (currentsection != string.empty) ? "_" : string.empty, line.split('=')[0].trim()), parsevalue(line.split('=')[1].trim().split(';')[0])); } } } } private object parsevalue(string value) { if (string.isnullorwhitespace(value)) return string.empty; if (value.startswith("\"") && value.endswith("\"")) return value.substring(1, value.length - 1); if (isnumeric(value)) return convert.toint32(value); // todo: fixme floating values (not confuse ipaddress). //if (isfloating(value)) // return convert.todouble(value); if (string.compare(value, "true", stringcomparison.ordinalignorecase) == 0) return true; if (string.compare(value, "false", stringcomparison.ordinalignorecase) == 0) return false; return value; } public override bool trygetmember(getmemberbinder binder, out object result) { if (this.dictionary.containskey(binder.name)) { if (this.dictionary[binder.name] == null) { result = null; } else { result = this.dictionary[binder.name]; } return true; } throw new configurationexception(binder.name); } public override string tostring() { string result = this.path + " [ "; int processed = 0; foreach (keyvaluepair<string, object> value in this.dictionary) { result += value.key; processed++; if (processed < this.dictionary.count) { result += ", "; } } result += " ]"; return result; } private static bool isnumeric(string value) { foreach (char c in value) if (!char.isdigit(c)) return false; return true; } private static bool isfloating(string value) { foreach (char c in value) if (!char.isdigit(c) && c != '.') return false; return true; } private class nullobject { } }
everything working flawlessly. i've overriden trygetmember
, i'm returning object based on getmemberbinder
name
property. however, i'm facing problem.
when execute following code:
string s = "some_config_key"; string d = configuration.s;
it retrieves value of key s
rather some_config_key
, meaning doesn't evalute value of variable s
. there workaround this?
thanks.
c# dynamic features partially compiled property/method access. understand lets take example.
dynamic myvar = new { a=1, b="test" }; myvar.a += 1;
here c# type system not test validity of property while compilation, @ runtime code executed , if property not found through runtime error.
so in code property access complied access property named "s" on dynamic object "configuration".
in c# no-way can not in dynamically typed languages python or javascript.
you have use this.
dynamic configuration = getconfig("path/to/file"); var myobj = configuration idictionary<string,object>; string s = "config_key"; var value = myobj[s]; // correct.
Comments
Post a Comment