如何获取数组属性的类型?

Nat*_*ium 1 .net reflection

我有这样的事情:

public class Foo
{
    public Bar[] Bars{get; set;}
}


public class Bar
{
    public string Name{ get; set; }
}
Run Code Online (Sandbox Code Playgroud)

我开始反思:

PropertyInfo propertyInfo = typeof(Foo).GetProperty("Bars");
Run Code Online (Sandbox Code Playgroud)

到现在为止还挺好.我想更深入地反思:

Type type = _propertyInfo .PropertyType; // this gives me that the type is Bar[]
Run Code Online (Sandbox Code Playgroud)

typeBar[],但它不是可以反映出type去找物业Name.有没有办法找出没有数组的trype?或者另一种找到单一类型Bar的方法?

Yur*_*ich 9

if (type.HasElementType)
    Console.WriteLine(type.GetElementType().Name);
Run Code Online (Sandbox Code Playgroud)

我写了HasElementType,因为我猜你也需要弄清楚你的元素是否是一个数组.