473,549 Members | 2,639 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Common ansestry to Fieldinfo and Propertyinfo

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(Membe rName)
[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.Fie ld:
((FieldInfo)MI) .SetValue(this,
ValueToSet);
break;
case MemberTypes.Pro perty:
((PropertyInfo) MI).SetValue(th is,
ValueToSet, null);
break;
default:
break;
}
}
Jun 27 '08 #1
1 4853
da**********@ya hoo.com.au <da**********@y ahoo.com.auwrot e:
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.GetVa lue/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 - <sk***@pobox.co m>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon_skeet
C# in Depth: http://csharpindepth.com
Jun 27 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
7203
by: Tony Tullemans | last post by:
I am trying to write a subroutine that will examine all the properties of a form to determine which of those are SqlCommand objects, and then set the Connection property of those SqlCommands to a passed-in SqlCommand object. I can find the properties OK but am unsuccessful in trying to change their value due to the way I have identified...
2
1412
by: dotnetguy | last post by:
Hello, having a FieldInfo object I'd like to get a reference to the corresponding field object. I know the type of the field object (so I can do a cast if it is required) but I don't know if FieldInfo indeed provides a reference to the underlying object and how to get it. Can anyone help out??? Thx.
0
2144
by: James Hadwen | last post by:
Hi, I'm trying to do something with reflection that's a little wierd. The idea is that I call for a field to be updated on a base class. Using reflection it first updates the value, and then updates the change that has been made to a dictionary. The code below is just to encourage some ideas, if you look at most of the code you find that...
2
2051
by: Picho | last post by:
Hi guys is it possible, once getting hold of a propertyInfo instance to know when the value of the property changed? that is without requiring the property to raise an event on change obviously... maybe something with the PropertyInfo.GetSetMethod()? monitoring when that method is called somehow?
3
3098
by: Laurie | last post by:
Hi, I need to be able to manipulate field values within a structure using FieldInfo.GetValue and FieldInfo.SetValue, in VB.Nt 2003. The GetValue is working fine and makes it really easy for me to maintain my code. Unfortunately, SetValue doesn't return any errors but doesn't actually set the value. I have read through various newsgroup...
15
8190
by: Charles Law | last post by:
I have adapted the following code from the MSDN help for PropertyInfo SetValue. In the original code, the structure MyStructure is defined as a class MyProperty, and it works as expected. There is also a minor change in class Mypropertyinfo, which I have commented out. When using a structure, however, the second call to GetValue returns...
5
3948
by: Dennis | last post by:
I have an array "t" where each element is a structure of type "testing". I want to use the FieldInfo class to set the values of the array element fields. I can read the field properties and return the velue using this technique but cannot set the field value as per below. Why won't it change the value. If define "testing" as a class where...
1
2191
by: David Gouge | last post by:
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...
2
2507
by: Carlos Rodriguez | last post by:
I have the following function in C#public void Undo(IDesignerHost host) { if (!this.componentName.Equals(string.Empty) && (this.member != null)) { IContainer container1 = (IContainer) host.GetService(typeof(IContainer)); IComponent component1 = container1.Components; PropertyInfo info1 = component1.GetType().GetProperty(this.member.Name);
0
7462
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7975
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7492
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
5101
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3510
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3491
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1957
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1069
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
777
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.