Get a property value with only the object and the name of the property (but not the type)

Posted by Vaccano on Stack Overflow See other posts from Stack Overflow or by Vaccano
Published on 2010-06-03T20:23:17Z Indexed on 2010/06/03 20:34 UTC
Read the original article Hit count: 119

Filed under:
|
|

Suppose I have a method that passes in the name of a property (as a string) and the object that the property is on (as object).

How could I get the value of the property?

Here is some code to make it a bit more concrete:

protected override void Paint(Graphics g, Rectangle bounds, CurrencyManager source, int rowNum, Brush backBrush, Brush foreBrush, bool alignToRight)
{
   // The next line is made up code
   var currentValue = source.Current.CoolMethodToTakePropertyNameAndReturnValue(MappingName);

   // Paint out the retrieved value
   g.DrawString(currentValue.ToString() , _gridFont, new SolidBrush(Color.Black), bounds.Left + 1, bounds.Top);
}

MappingName is the name of the property I want to get the value for. What I need is CoolMethodToTakePropertyNameAndReturnValue.

Any ideas? I am running on the Compact Framework. I would also prefer to avoid reflection (but if that is my only recourse then so be it).

Thanks for any help.

© Stack Overflow or respective owner

Related posts about c#

Related posts about .NET