patterncsharpCritical
Get property value from string using reflection
Viewed 0 times
propertyfromusingreflectionvaluestringget
Problem
I am trying implement the Data transformation using Reflection1 example in my code.
The
Is this possible?
1 Web Archive version of original blog post
The
GetSourceValue function has a switch comparing various types, but I want to remove these types and properties and have GetSourceValue get the value of the property using only a single string as the parameter. I want to pass a class and property in the string and resolve the value of the property.Is this possible?
1 Web Archive version of original blog post
Solution
public static object GetPropValue(object src, string propName)
{
return src.GetType().GetProperty(propName).GetValue(src, null);
}Of course, you will want to add validation and whatnot, but that is the gist of it.
Code Snippets
public static object GetPropValue(object src, string propName)
{
return src.GetType().GetProperty(propName).GetValue(src, null);
}Context
Stack Overflow Q#1196991, score: 2291
Revisions (0)
No revisions yet.