FieldInfo class

  • Thread starter Thread starter David Gouge
  • Start date Start date
D

David Gouge

Posting on behalf of a colleague (no, really! ;) )...

Hi,

I was wondering if there is any equivalent of FieldInfo.SetValue(Object,
Object) that is strongly typed or uses generics? I have been looking at
the source for FieldInfo to see if I could implement my own generic
version of this method but unfortunately the code seems to be hidden, is
there anyway I can implement my own method if there is no equivalent?

TIA!
 
David,

You could easily write a wrapper, like so:

public static SetValue<T>(object obj, FieldInfo fieldInfo, T value)
{
// Set the value.
fieldInfo.SetValue(obj, value);
}

public static T GetValue<T>(object obj, FieldInfo fieldInfo)
{
// Get the value and return.
return (T) fieldInfo.GetValue(obj);
}

Hope this helps.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top