的GetType()对数组项? [英] GetType() on Array item?
本文介绍了的GetType()对数组项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个初始化数组,可能不包含的项目。
I have an initialised array that may contain no items.
让我们把它叫做 A
,
在电话
显然会返回一个类型数组。是否有可能获得该数组包含?的GetType()
A
Calling GetType()
on a
will obviously return a type of Array. Is it possible to get the type of the items the array contains?
显然 A [0] .GetType()
将工作,但随后的阵列可以是空的,导致空引用异常。
Obviously a[0].GetType()
would work, but then the array could be empty and cause a null reference exception.
推荐答案
好了,你可以得到的数组的元素类型:
Well, you can get the element type of the array:
Type type = array.GetType().GetElementType();
(这是不太一样获得类型数组中的项目 - 一个 [对象]
可完全处理字符串填充,例如)
(That's not quite the same as getting the types of the items in the array - an object[]
may be entirely populated with strings, for example.)
这篇关于的GetType()对数组项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文