使用反射从字符串中获取属性值 [英] Get property value from string using reflection
本文介绍了使用反射从字符串中获取属性值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试实施使用反射的数据转换1 示例在我的代码中.
I am trying implement the Data transformation using Reflection1 example in my code.
GetSourceValue
函数有一个比较各种类型的开关,但我想删除这些类型和属性,并让 GetSourceValue
仅使用单个字符串获取属性值作为参数.我想在字符串中传递一个类和属性并解析该属性的值.
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.
这可能吗?
推荐答案
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.
这篇关于使用反射从字符串中获取属性值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文