数组项上的 GetType()? [英] GetType() on Array item?

查看:27
本文介绍了数组项上的 GetType()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个可能不包含任何项目的初始化数组.

I have an initialised array that may contain no items.

姑且称之为a,

a 上调用 GetType() 显然会返回一个数组类型.是否可以获取数组包含的项的类型?

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.

推荐答案

好吧,您可以获得 数组元素类型:

Type type = array.GetType().GetElementType();

(这与获取数组中项目的类型不太一样——例如,object[] 可能完全填充了字符串.)

(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屋!

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