Reflection - Get Property

RobEmDee

Centurion
Joined
Mar 25, 2003
Messages
130
Location
Richmond, VA
How can Reflection be used to access the Property of an object through it's string representation? Similar to the functionality of the ValueMember, DisplayMember Properties for the ListControl object.
 
RobEmDee said:
How can Reflection be used to access the Property of an object through it's string representation? Similar to the functionality of the ValueMember, DisplayMember Properties for the ListControl object.
given a type "MyType" implements ICollection and has a property "MyProperty" that returns a MyOtherType objects. . .


C#:
MyType myType = new MyType();
Type type = myType.GetType();
 
/* object obj = myType[1]; */
PropertyInfo propInfo = type.GetProperty("Item");
object obj = propInfo.Invoke(myType, new Object[]{1}); 
 
/* MyOtherType myOtherType = myType.MyProperty;*/
propInfo = type.GetProperty("MyProperty");
MyOtherType myOtherType = (MyOtherType) propInfo.Invoke(myType, null);
 
Back
Top