获得在C#中的特定对象属性的值而无需知道背后的类 [英] Get value of a specific object property in C# without knowing the class behind

查看:104
本文介绍了获得在C#中的特定对象属性的值而无需知道背后的类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个对象类型(.NET)的目标。我不知道真正的类型(类)后面运行时,但我知道,该对象有一个属性字符串名称。我怎样才能retrive名的价值? ?这是可能的。



是这样的:

 对象项目= AnyFunction(....); 
字符串值= item.name;


解决方案

使用反射

  System.Reflection.PropertyInfo PI = item.GetType()的getProperty(名称)。 
字符串名称=(字符串)(pi.GetValue(项目,NULL));


I have an object (.NET) of type "object". I don't know the "real type (class)" behind it during runtime , but I know, that the object has a property "string name". How can I retrive the value of "name"? Is this possible?

something like this:

object item = AnyFunction(....);
string value = item.name;

解决方案

Use reflection

System.Reflection.PropertyInfo pi = item.GetType().GetProperty("name");
String name = (String)(pi.GetValue(item, null));

这篇关于获得在C#中的特定对象属性的值而无需知道背后的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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