如何使用反射来确定数组的嵌套式(单元式)?嵌套、数组、如何使用、射来

由网友(青墨ジ断笺)分享简介:我的System.Type的一个实例,对于其中IsArray的返回true。我如何能够确定数组类型的嵌套式?即。 键入GetArrayType(T型){如果(t.IsArray){//什么放在这里?}抛出新的异常(类型不是数组);}Assert.That(GetArrayType(typeof运算(字符串[]))...

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

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

即。

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

解决方案

  t.GetElementType()
 

Reference.

C语言,用两种方法实现,方法一 嵌套循环,方法二 数组

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.

阅读全文

相关推荐

最新文章