本文介绍了使用反射从字符串获取属性值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试实现使用反射进行数据转换 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.
这可能吗?
1 原始博客文章的网络存档版本
推荐答案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.
更多推荐
使用反射从字符串获取属性值
发布评论