假设我有一个System.String[]
类型的对象.我可以查询type对象以确定它是否是数组
Type t1 = typeof(System.String[]);
bool isAnArray = t1.IsArray; // should be true
但是,如何从t1获取数组项的类型对象
Type t2 = ....; // should be typeof(System.String)
假设我有一个System.String[]
类型的对象.我可以查询type对象以确定它是否是数组
Type t1 = typeof(System.String[]);
bool isAnArray = t1.IsArray; // should be true
但是,如何从t1获取数组项的类型对象
Type t2 = ....; // should be typeof(System.String)
为此,可以使用实例方法Type.GetElementType
.
Type t2 = t1.GetElementType();
[返回]当前数组、指针或引用类型包含或引用的对象的类型,如果当前类型不是数组或指针,或未通过引用传递,或表示泛型类型或泛型方法定义中的泛型类型或类型参数,则返回null.