Int32[] myArray = new Int32[0];
//Somewhere else in code:
Type t = myArray.GetType();
//t == Int32[]
//element type of t == ???
如何找出创建t存储的元素类型。
我发现的唯一示例适用于不为空的数组,您只需在其中执行myArray [i] .GetType()。那么,对于数组长度为0的情况,您该怎么做?
仅供参考:我做了以下事情,它工作正常,但是哇...它使用字符串转换,而且非常丑陋。有一种更好的方法:
Type t = myArray.GetType();
string strT = t.ToString();
string strArrayBase = strT.Substring(0, strT.Length - 2);
Type elementType = Type.GetType(strArrayBase);
答案 0 :(得分:1)
您可以使用.GetElementType()
例如:
> int[] arr = new int[0];
> arr.GetType().GetElementType()
[System.Int32]