开发者

Reflection a properties of type of generic list

i have开发者_StackOverflow a class like below

public class Foo<T>
{
  public List<T> Items{ get; set; }
}

and

i have a instance that above class,

Foo<Bar> bars = GetBars();

how i can get properties of Bar using reflection?

i try this

PropertyInfo[] properties = bars.Items.First().GetType().GetProperties();

but i think,its not good way,is there any better way do this?


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

Assuming you don't know the type the list will contain.

If it'll always be a Bar then use typeof(Bar).GetProperties();


try:

var properties = typeof(Bar).GetProperties();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜