PC Review


Reply
Thread Tools Rate Thread

Common ansestry to Fieldinfo and Propertyinfo

 
 
damiensawyer@yahoo.com.au
Guest
Posts: n/a
 
      23rd Jun 2008
Hi,

I need to invoke .getvalue and .setvalue on fieldinfo and propetyinfo
objects. Even though they're both derived from MemberInfo, they don't
share those two methods. I've finding myself writing the following
types of structure over and over. Is there something I'm missing? Or
some 'funky' 3.5 delegate/lambda expression way of doing this?

Thanks very much in advance,


Damien


MemberInfo MI = this.GetType().GetMember(MemberName)
[0];
object ValueToSet = oDT.Rows[0][col.ColumnName] ??
"";
try
{
//It doesn't seem that Fieldinfo and Propertyinfo
// have a common ancestry, therefore no common map
to .SetValue.
switch (MI.MemberType)
{
case MemberTypes.Field:
((FieldInfo)MI).SetValue(this,
ValueToSet);
break;
case MemberTypes.Property:
((PropertyInfo)MI).SetValue(this,
ValueToSet, null);
break;
default:
break;
}
}
 
Reply With Quote
 
 
 
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      23rd Jun 2008
(E-Mail Removed) <(E-Mail Removed)> wrote:
> I need to invoke .getvalue and .setvalue on fieldinfo and propetyinfo
> objects. Even though they're both derived from MemberInfo, they don't
> share those two methods. I've finding myself writing the following
> types of structure over and over. Is there something I'm missing? Or
> some 'funky' 3.5 delegate/lambda expression way of doing this?


Bear in mind that they don't have the same parameters either -
FieldInfo.GetValue/SetValue doesn't take a set of arguments as one of
its parameters, whereas PropertyInfo does. In other words, it's hard to
see how they *would* share methods, even if there was some intermediate
common type (or an interface). I guess you could have PropertyInfo with
an overload of GetValue/SetValue which assumed no parameters.

You could always write a pair of utility methods to do this if you find
yourself doing it often though.

--
Jon Skeet - <(E-Mail Removed)>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon_skeet
C# in Depth: http://csharpindepth.com
 
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
Using Reflection on FieldInfo Ron Microsoft C# .NET 5 13th Mar 2008 07:32 PM
FieldInfo =?Utf-8?B?U8O2cmVuX01hcm9kw7ZyZW4=?= Microsoft Excel Programming 2 28th Sep 2007 12:10 PM
FieldInfo GPO Microsoft Excel Programming 2 15th Jul 2004 06:47 AM
FieldInfo.SetValue() =?Utf-8?B?R2xlbg==?= Microsoft C# .NET 3 18th Jun 2004 09:08 PM
Reflection, FieldInfo and ref James Hadwen Microsoft C# .NET 0 27th Apr 2004 05:56 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 12:43 PM.