> 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();
[quoted text clipped - 3 lines]
> new MemberFilter(DelegateToSearchCriteria), "ReferenceEquals");
> PropertyInfo pi = t.GetProperty(arrayMemberInfo[0].Name);
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.

Signature
.NET 2.0 for Delphi Programmers <http://www.midnightbeach.com/.net>
Delphi skills make .NET easy to learn
Just printed, and shipping now.
AdamM - 06 Jun 2006 06:32 GMT
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
>> How do you take a PropertyInfo and get an instance of the object
>> described
[quoted text clipped - 33 lines]
> instance. All the pointers are one way, instance to type, not type to
> instances.
Jon Shemitz - 06 Jun 2006 07:35 GMT
> So my ultimate problem is that I have classes with some members that are
> SortedLists.
[quoted text clipped - 5 lines]
>
> Perhaps I am approaching the problem wrong?
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.

Signature
.NET 2.0 for Delphi Programmers <http://www.midnightbeach.com/.net>
Delphi skills make .NET easy to learn
Just printed, and shipping now.
Ben Voigt - 06 Jun 2006 14:13 GMT
>> So my ultimate problem is that I have classes with some members that are
>> SortedLists.
[quoted text clipped - 8 lines]
>>
>> Perhaps I am approaching the problem wrong?
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; }
}
}
> 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.