Yes; although you would need to cast
Dictionary<string, string> dicTemp1 = (Dictionary<string, string>)
fieldInfo.GetValue(this);
So yes: if you have multiple fields on the current instance called different
things, then this approach might work, i.e. if "dictionary1" is replaced
with a parameter; consider:
public class SomeClass {
private Dictionary<string, string> dictionary1, dictionary2, dictionary3,
dictionary4;
}
Then the code should work with any of "dictionary1"..."dictionary4" as
arguments to GetField (except that the fields are private, so probably won't
be found with GetField(blah)); however, the following approach would be more
efficient:
private Dictionary<string, string> GetDictionary(string fieldName) {
switch(fieldName) {
case "dictionary1": return dictionary1;
case "dictionary2": return dictionary2;
case "dictionary3": return dictionary3;
case "dictionary4": return dictionary4;
default: throw new ArgumentException("Dictionary " + fieldName + " not
recognised");
}
Additionally, this avoids the problems with the fields being private (which
needs a different overload to GetField); a tiny bit more effort, but much,
much quicker.
Marc
|