在C#中使用反射从字符串获取属性值 [英] Get property value from string using reflection in C#

查看:411
本文介绍了在C#中使用反射从字符串获取属性值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现使用反射 1 改造SUP>例如在我的code。

GetSourceValue 函数比较不同类型的交换机,但我要删除这些类型和属性,并有 GetSourceValue 获得仅使用单个字符串为参数的属性的值。我想通过在字符串中类和属性并解决财产的价值。

这可能吗?

1 <分> Web原来的博客文章存档版本


解决方案

 公共静态对象GetPropValue(SRC对象,字符串作为propName)
 {
     返回src.GetType()的getProperty(作为propName).GetValue(SRC,NULL);
 }

当然,你将要添加验证和诸如此类的东西,但这是它的精神。

I am trying implement the Data transformation using Reflection1 example in my code.

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

解决方案

 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.

这篇关于在C#中使用反射从字符串获取属性值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆