开发者

How to get the type of an array-property?

I have something like this:

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


public class Bar
{
    public string Name{ get; set; }
}

I start reflecting:

PropertyInfo propertyInfo = typeof(Foo).GetProperty("Bars");

so far so good. I want to reflect deeper:

Type type = _propertyInfo .PropertyType; // this gives me that the type is Bar[]

The type is Bar[], but it is not possible to reflect on type to开发者_如何学运维 look for property Name. Is there a way to figure out the trype without the array? Or another way to find the single type of Bar?


if (type.HasElementType)
    Console.WriteLine(type.GetElementType().Name);

I wrote the HasElementType because I'm guessing you are also going to need to figure out if your element is an array.


type.GetElementType().Name
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜