开发者

How to get the type of an array-property?

开发者 https://www.devze.com 2022-12-12 01:27 出处:网络
I have something like this: public class Foo { public Bar[] Bars{get; set;} } public class Bar { public string Name{ get; set; }

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

精彩评论

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