泛型列表类型的思考一个特性特性、类型、列表

由网友(泪花了淡妆〆)分享简介:我有一类像下面public class Foo{public List Items{ get; set; }}和我有一个实例上面的类,i have a instance that above class,Foo bars = GetBars();我怎么可以使用反射得到律师的性质?ho...

我有一类像下面

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();

阅读全文

相关推荐

最新文章