反映通用列表类型的属性

tob*_*ias 1 .net c# generics reflection

我有一个类似下面的课程

public class Foo<T>
{
  public List<T> Items{ get; set; }
}
Run Code Online (Sandbox Code Playgroud)

我有一个上面的实例,

Foo<Bar> bars = GetBars();
Run Code Online (Sandbox Code Playgroud)

我如何使用反射获得Bar的属性?

我试试这个

PropertyInfo[] properties = bars.Items.First().GetType().GetProperties();
Run Code Online (Sandbox Code Playgroud)

但我认为,这不是一个好方法,有没有更好的方法呢?

Geo*_*ett 6

var Properties = bars.GetType().GetGenericArguments()[0].GetProperties();

假设您不知道列表将包含的类型.

如果它永远是一个Bar使用typeof(Bar).GetProperties();