泛型列表类型的思考一个特性 [英] Reflection a properties of type of generic list
本文介绍了泛型列表类型的思考一个特性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一类像下面
public class Foo<T>
{
public List<T> Items{ get; set; }
}
和
我有一个实例上面的类,
i have a instance that above class,
Foo<Bar> bars = GetBars();
我怎么可以使用反射得到律师的性质?
how i can get properties of Bar using reflection?
我尝试这个
PropertyInfo[] properties = bars.Items.First().GetType().GetProperties();
但我认为,它不是好办法,有没有更好的办法做到这一点?
but i think,its not good way,is there any better way do this?
推荐答案
VAR属性= bars.GetType()GetGenericArguments()[0] .GetProperties();
假设你不知道该列表将包含的类型。
Assuming you don't know the type the list will contain.
如果它永远是一个酒吧
然后用的typeof(酒吧).GetProperties();
If it'll always be a Bar
then use typeof(Bar).GetProperties();
这篇关于泛型列表类型的思考一个特性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文