如何使用反射来确定数组的嵌套式(单元式)? [英] How do I use reflection to determine the nested type (element type) of an array?

查看:69
本文介绍了如何使用反射来确定数组的嵌套式(单元式)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的System.Type的一个实例,对于其中IsArray的返回true。

我如何能够确定数组类型的嵌套式?

即。

 键入GetArrayType(T型)
{
    如果(t.IsArray)
    {
        //什么放在这里?
    }
    抛出新的异常(类型不是数组);
}
Assert.That(GetArrayType(typeof运算(字符串[])),Iz.EqualTo(typeof运算(字符串));
Assert.That(GetArrayType(typeof运算(富[])),Iz.EqualTo(typeof运算(富));
 

解决方案

  t.GetElementType()
 

<一个href="http://msdn.microsoft.com/en-us/library/system.type.getelementtype%28VS.71%29.aspx">Reference.

I have an instance of System.Type, for which "IsArray" returns true.

How can I determine the "nested type" of the array type?

i.e.

Type GetArrayType(Type t)
{
    if(t.IsArray)
    {
        //  What to put here?
    }
    throw new Exception("Type is not an array");
}
Assert.That(GetArrayType(typeof(string[])), Iz.EqualTo(typeof(string));
Assert.That(GetArrayType(typeof(Foo[])), Iz.EqualTo(typeof(Foo));

解决方案

t.GetElementType()

Reference.

这篇关于如何使用反射来确定数组的嵌套式(单元式)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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