PC Review


Reply
Thread Tools Rate Thread

Convert FieldInfo to Dictionary<string, string>

 
 
=?Utf-8?B?V2lsc29u?=
Guest
Posts: n/a
 
      22nd Jun 2006
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

 
Reply With Quote
 
 
 
 
Marc Gravell
Guest
Posts: n/a
 
      22nd Jun 2006
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


 
Reply With Quote
 
Marc Gravell
Guest
Posts: n/a
 
      22nd Jun 2006
typo: fieldInfo.GetValue({instance});

Marc


 
Reply With Quote
 
Marc Gravell
Guest
Posts: n/a
 
      22nd Jun 2006
and [dic] should have been [sic]... hmm: a typo in sic... vaguely ironic...

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

Marc


 
Reply With Quote
 
=?Utf-8?B?V2lsc29u?=
Guest
Posts: n/a
 
      22nd Jun 2006
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,string>'

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
>
>
>

 
Reply With Quote
 
Marc Gravell
Guest
Posts: n/a
 
      22nd Jun 2006
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


 
Reply With Quote
 
Marc Gravell
Guest
Posts: n/a
 
      22nd Jun 2006
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


 
Reply With Quote
 
=?Utf-8?B?V2lsc29u?=
Guest
Posts: n/a
 
      22nd Jun 2006
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
>
>
>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
databind a gridview to a dictionary<string, string> only shows 1 row? Andy B Microsoft ASP .NET 2 26th Apr 2008 07:55 PM
databinding checkBoxList to a Dictionary<string, string> object Andy B Microsoft ASP .NET 0 21st Apr 2008 07:40 PM
serialize dictionary<string, string> collection in .net3.5 Andy B Microsoft C# .NET 6 7th Apr 2008 12:57 PM
Create Permutations from Dictionary<string, List<string>> Assimalyst Microsoft C# .NET 2 30th Nov 2007 07:50 PM
Convert Dictionary<string,SomeType> keys to List<string> buzzweetman@gmail.com Microsoft C# .NET 6 9th Aug 2006 03:54 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 05:39 PM.