0

Given an object and the name of a property, I'm trying to use reflection to get the value of that property. I've seen several threads here that are getting me close, but I'm not nailing it just yet. I happen to know that the value will be of type string.

public string GetPropValue(object src, string propName)
{
    return src.GetType().GetProperty(propName).GetValue(????);
}
Casey Crookston
  • 13,016
  • 24
  • 107
  • 193
  • That thread is one that I looked at carefully. But in that example, the method is returning an object, and not a string which would be the value of propName. I'm trying to modify that example just a little. – Casey Crookston Aug 25 '16 at 16:40
  • Sorry, but it really is duplicate. You can adjust the answer from the related question for your needs. You can convert the returned `object` to `string` the same way as you would do that for regular `object` variable, thus that part has nothing in common with reflection. – Ivan Stoev Aug 25 '16 at 16:49

1 Answers1

1

Just pass the src object like this:

public string GetPropValue(object src, string propName)
{
    return src.GetType().GetProperty(propName).GetValue(src, null).ToString();
}
Umair M
  • 10,298
  • 6
  • 42
  • 74