我如何从数组类型的数组项目类型在.NET [英] How do I get the Array Item Type from Array Type in .net

查看:128
本文介绍了我如何从数组类型的数组项目类型在.NET的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有一个 System.String [] 类型的对象。我可以查询类型的对象,以确定它是否是一个数组

 类型T1 = typeof运算(System.String []);
布尔isAnArray = t1.IsArray; //应该是真实的
 

不过我怎么获取数组项目从T1类型的对象

 键入T2 = ....; //应的typeof(System.String)
 

解决方案

您可以使用实例方法<一href="http://msdn.microsoft.com/en-us/library/system.type.getelementtype.aspxtype."><$c$c>Type.GetElementType用于此目的。

 键入T2 = t1.GetElementType();
 

  

[返回]包含或由当前的阵列,指针或引用类型,或空称为如果当前类型不是数组或指针,或不被引用传递,或重新$的对象的类型p $ psents泛型类型或一个泛型类型或泛型方法定义的类型参数。

Say I have an System.String[] type object. I can query the type object to determine if it is an array

Type t1 = typeof(System.String[]);
bool isAnArray = t1.IsArray; // should be true

However how do I get a type object of the array item from t1

Type t2 = ....; // should be typeof(System.String)

解决方案

You can use the instance method Type.GetElementType for this purpose.

Type t2 = t1.GetElementType();

[Returns] the type of the object encompassed or referred to by the current array, pointer, or reference type, or null if the current Type is not an array or a pointer, or is not passed by reference, or represents a generic type or a type parameter in the definition of a generic type or generic method.

这篇关于我如何从数组类型的数组项目类型在.NET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆