How to get the value of: MyObject.Property1 by name. Use reflection?

Hi all,

I don't know the name of my property until runtime so I want to get the value by the property's name.

Psuedo Ex.

Return MyObject."Property Name Goes Here"

-Andy

[242 byte] By [Andeezle] at [2007-12-20]
# 1

Reflection is what you want, but is a bit more involved than your code... The documentation is pretty good on this, as are articles on codeProject or other sites.

/* assume obj is an object of type MyClass */

Type type = Type.GetType("MyClass");
PropertyInfo property = type.GetProperty("FontType");
property.SetValue( obj, 10,
null );

Int32 value = (Int32)property.GetValue( obj, null );
Debug.Assert( value == 10 );

JohnArlen at 2007-9-9 > top of Msdn Tech,.NET Development,.NET Base Class Library...

.NET Development

Site Classified