使用反射从字符串获取属性值 [英] Get property value from string using reflection
本文介绍了使用反射从字符串获取属性值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试实现使用反射进行数据转换 <我的代码中的sup> 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屋!
查看全文