HELP! need reflection help

  • Thread starter Thread starter M
  • Start date Start date
M

M

Given string propertyName, I want to return the value of a property,
like so:

getPropertyValueByFieldName(string propertyName, object myClass){}

Can someone please give me the syntax required for reflecting on a class
this way?

So far I can't get this to work:



object propertyValue =

myClass.GetType().GetProperty(Convert.ToString(propertyName)).GetValue(myClass,null);
 
This seems to work ok.
http://tinyurl.com/rtpn




public object getFieldByPropertyName(string propertyName, object myClass)
{
Type t = myClass.GetType();
object v;
v = (object) t.InvokeMember(propertyName,
BindingFlags.DeclaredOnly |
BindingFlags.Public | BindingFlags.NonPublic |
BindingFlags.Instance | BindingFlags.GetProperty, null, myClass, null);

return v;
}
 
Back
Top