Groups | Blog | Home
all groups > dotnet clr > june 2006 >

dotnet clr : Reflection question about PropertyInfo


AdamM
6/5/2006 9:11:19 PM
Hi all,

How do you take a PropertyInfo and get an instance of the object described
by it? For example, I want to get the actual instance of the object
described by "pi" in the snippet below (not the object's value like what
GetValue() provides, but a reference to the object itself):
MemberInfo[] arrayMemberInfo;

Type t = tmptarget.GetType();

arrayMemberInfo = t.FindMembers(MemberTypes.Property,

BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance |
BindingFlags.SetProperty,

new MemberFilter(DelegateToSearchCriteria), "ReferenceEquals");

PropertyInfo pi = t.GetProperty(arrayMemberInfo[0].Name);

Thanks in advance!

Adam

Jon Shemitz
6/5/2006 9:46:22 PM
[quoted text, click to view]

You mean how do you go from a value like "pi" to a value like "t"? You
don't.

A Type value is a runtime description of a class. It's the runtime
equivalent of a compiletime class, and a Type value is as distinct
from an instance of the class as the compiletime class is distinct
from an instance of the class.

A PropertyInfo value is a runtime description of a property, which is
a class member. To call GetValue, you need to supply an instance
reference, like your "tmptarget".

You can go from a PropertyInfo to its ReflectedType, which is the Type
of the class which contains the property. You can go from a
PropertyInfo to its DeclaringType, which is the Type of the class
which contains the property. (If the ReflectedType is not the same as
the DeclaringType, the ReflectedType inherited the member from the
DeclaringType.)

But you can't go from a PropertyInfo to an instance any more than you
can go from a Type to an instance or from a compiletime class to an
instance. All the pointers are one way, instance to type, not type to
instances.

--

..NET 2.0 for Delphi Programmers <http://www.midnightbeach.com/.net>

Delphi skills make .NET easy to learn
AdamM
6/5/2006 10:32:51 PM
Hi Jon,

Thanks for the clarification.

So my ultimate problem is that I have classes with some members that are
SortedLists.
At runtime, how can I use reflection to identify and retrieve the contents
of these SortedList members and display/manipulate their contents? For
example, my type (tmptarget) has a SortedList member called tmptarget.List.
I want to enumerate through it's members and locate the SortedList (List) at
runtime without knowing in advance the name of the SortedList.

Perhaps I am approaching the problem wrong?

Thanks!
Adam


[quoted text, click to view]

Jon Shemitz
6/5/2006 11:35:11 PM
[quoted text, click to view]

Perhaps. Quickly (because I have to get up at 5, to volunteer all days
at the polls tomorrow) you could certainly use Type.FindMembers to
find all fields and/or properties of type SortedList.

--

..NET 2.0 for Delphi Programmers <http://www.midnightbeach.com/.net>

Delphi skills make .NET easy to learn
Ben Voigt
6/6/2006 8:13:29 AM
[quoted text, click to view]

It sounds as if you are looking for some mixture of PropertyInfo (or
PropertyDescriptor) and delegates, that captures the object instance and all
its type descriptor information. I've added delegates because I think they
will be far faster than calling PropertyInfo.GetValue() for instance. With
generics, this really isn't too hard to accomplish (not tested):

class PropertyDelegate<S, T>
{
private readonly S instance;
private readonly PropertyInfo prop;

private delegate T GetAccessor();
private delegate void SetAccessor(T value);

private readonly GetAccessor getter;
private readonly SetAccessor setter;

public PropertyDelegate(S for, PropertyInfo propinfo)
{
instance = for;
prop = propinfo;
if (!propinfo.DeclaringType.IsAssignableFrom(typeof(S))) throw new
ClassCastException();
if (!typeof(T).IsAssignableFrom(propinfo.PropertyType) throw new
ClassCastException();
getter = (GetAccessor)Delegate.CreateDelegate(typeof(GetAccessor),
for, propinfo.GetGetMethod(), false);
setter = (SetAccessor)Delegate.CreateDelegate(typeof(SetAccessor),
for, propinfo.GetSetMethod(), false);
}

public T Value {
get { return getter(); }
set { setter(value); }
}

public string Name {
get { return prop.Name; }
}
}

[quoted text, click to view]

AddThis Social Bookmark Button