的GetType()对数组项?

编程入门 行业动态 更新时间:2024-10-22 19:40:45
本文介绍了的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()对数组项?

本文发布于:2023-07-29 00:39:32,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1235608.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   GetType

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!