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
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
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");Int32 value = (Int32)property.GetValue( obj, null );
Debug.Assert( value == 10 );