all groups > dotnet clr > january 2005 >
You're in the

dotnet clr

group:

Get PropertyInfo via FieldInfo?



Get PropertyInfo via FieldInfo? darndt
1/29/2005 1:33:01 PM
dotnet clr: Given a FieldInfo for a given instance variable, how can I then get
PropertyInfo for a member of that object (assuming a type)...

To illustrate:

DoReflectionTask( object theForm, string theField )
{
/// Get the field info for a field on the form object

FieldInfo afi = theForm.GetType().GetField( theField, ... );

/// Assuming this is a Control to keep illustration simple...
/// How would I then get access to the DataBindings property
/// usng the FieldInfo structure above?

///.... Is this even possible?

}


Re: Get PropertyInfo via FieldInfo? Jon Shemitz
1/29/2005 1:40:56 PM
[quoted text, click to view]

Sounds like you want FieldInfo.DeclaringType - the Type the field is a
member of.

--

Re: Get PropertyInfo via FieldInfo? darndt
1/29/2005 2:19:01 PM
Joe,

Actually, it looks like this will do the trick, to get the property, i.e., -

PropertyInfo aPropInfo = aFieldInfo.FieldType.GetProperty("DataBindings");

But now the paradox:

If I want to GetValue() or Invoke the Get method of the property, I need the
Object for the FieldInfo! (In my example, the TextBox object itself.)

How does one get the actual object of the type returned by GetField().

(This stuff is hard to explain!)

Thanks,

- dave



[quoted text, click to view]
Re: Get PropertyInfo via FieldInfo? Mattias Sjögren
1/30/2005 2:23:26 AM

[quoted text, click to view]

FieldInfo.GetValue()



Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Re: Get PropertyInfo via FieldInfo? darndt
1/31/2005 8:25:22 AM
Ah! I was looking at the return value in the debugger and it was showing me
the TextBox text value - so I though it was retrieving some default value
from the object, rather than the object itself!

A simple trace out of the object's type name revealed that it is indeed the
text box object.

OK!


AddThis Social Bookmark Button