如何获取属性信息. [英] How to Get Properties information.
问题描述
亲爱的大家,
我创建了一个假设为Employee的类.该课程包括3个
属性,例如ID,名称,薪水.通过使用该类,我创建了雇员列表.
像List< employee> list = new List< employee>();
从该列表中,我想获取className,属性计数,属性名称以及
请给我任何想法.我想以一种通用的热情来创造它.我的意思是
List< t> list = new List< t>(); T可以是任何类或对象.
在此先谢谢您.
Dear Everybody,
I have created one class assume Employee. that class consists of 3
properties like Id, Name, Salary. By using that class i created list of Employees.
Like List<employee> list = new List<employee>();
from that list i want to get className , properties count, Properties Name aswell
Please give me any ideas. I want to create that in a Generic passion. what i mean is
List<t> list = new List<t>(); T can be any class or object.
Thanks in advance.
推荐答案
您需要使用Reflection ^ ]
但是,使用反射存在一些缺点.根据您的要求,最好使用一个接口来确保对象上的某些方法和属性可用.尝试使用泛型和反射来创建一种适合所有应用程序的大小的方法已尝试了多次,但均未成功.
You would need to use Reflection http://msdn.microsoft.com/en-us/library/ms173183(v=vs.80).aspx[^]
However, there are drawbacks to using Reflection. Depending on your requirements it may be better to use an interface to guarantee certain methods and properties are available on the objects. Trying to use generics and reflection to create a one size fits all application has been tried many times without success.
这篇关于如何获取属性信息.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!