HELP! need reflection help

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);
 
M

M

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;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top