471,123 Members | 978 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,123 software developers and data experts.

Convert FieldInfo to Dictionary<string, string>

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
7 8267
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
typo: fieldInfo.GetValue({instance});

Marc
Jun 22 '06 #3
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
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
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
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
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.

Similar topics

4 posts views Thread by Joanna Carter \(TeamB\) | last post: by
4 posts views Thread by Mark Rae | last post: by
6 posts views Thread by Paul.N.Phillips | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.