【问题标题】:How do I use reflection to determine the nested type (element type) of an array?如何使用反射来确定数组的嵌套类型(元素类型)?
【发布时间】:2009-05-08 17:12:07
【问题描述】:

我有一个 System.Type 实例,"IsArray" 返回 true。

如何判断数组类型的“嵌套类型”?

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));

【问题讨论】:

    标签: c# .net reflection


    【解决方案1】:
    t.GetElementType() 
    

    Reference.

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-04-20
      • 1970-01-01
      • 2021-09-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多