By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,082 Members | 2,108 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,082 IT Pros & Developers. It's quick & easy.

Convert FieldInfo to Dictionary<string, string>

P: n/a
Hi,

How do get the Dictioanry object from FiedlInfo ?

my code :
fieldInfo = this.GetType().GetField("dictioanry1");
??Dictionary<string, string> dicTemp1 = (Dictionary<string, string>)fieldInfo;

Thanks
Wilson

Jun 22 '06 #1
Share this Question
Share on Google+
7 Replies


P: n/a
The fieldInfo only knows about the type, not the instance: assuming that
fieldInfo isn't null (which suggests the field is public - is this really
correct?), then:

field.GetValue(this);

would return the value of the field in the current instance (since you are
using this.GetType() I assume this is what you want).

However, if "this" is correct (i.e. you really do mean the current
instance), then you can just use "dictioanry1" [dic] directly, without any
reflection?

dicTemp1 = dictioanry1;

Marc
Jun 22 '06 #2

P: n/a
typo: fieldInfo.GetValue({instance});

Marc
Jun 22 '06 #3

P: n/a
and [dic] should have been [sic]... hmm: a typo in sic... vaguely ironic...

Two typos in one short post: time for coffee...

Marc
Jun 22 '06 #4

P: n/a
Hello Marc,

Do you mean I need to change to :

fieldInfo = this.GetType().GetField("dictioanry1");
Dictionary<string, string> dicTemp1 = fieldInfo.GetValue(this);

Compiling error :
Cannot implicitly convert type 'object' to
'System.Collections.Generic.Dictionary<string,stri ng>'

I need this because "dictionary1" is a string parameters only. I don't know
which dictionary until runtime.

Thanks
Wilson
"Marc Gravell" wrote:
The fieldInfo only knows about the type, not the instance: assuming that
fieldInfo isn't null (which suggests the field is public - is this really
correct?), then:

field.GetValue(this);

would return the value of the field in the current instance (since you are
using this.GetType() I assume this is what you want).

However, if "this" is correct (i.e. you really do mean the current
instance), then you can just use "dictioanry1" [dic] directly, without any
reflection?

dicTemp1 = dictioanry1;

Marc

Jun 22 '06 #5

P: n/a
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
Jun 22 '06 #6

P: n/a
Oh, and you could also use an enum instead of a string as the argument; this
is quicker (switch on string compiles to a hashtable) and provides more
safety in that it is harder to call it with invalid arguments.

Marc
Jun 22 '06 #7

P: n/a
Thank you very much.

"Marc Gravell" wrote:
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

Jun 22 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.