假设我有一个 System.String[]
类型对象。我可以查询类型对象以确定它是否是数组
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();
[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 - 如何从 .net 中的数组类型获取数组项类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4129831/