c# - 如何使用反射来确定数组的嵌套类型(元素类型)?

标签 c# .net reflection

我有一个 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));

最佳答案

t.GetElementType() 

Reference .

关于c# - 如何使用反射来确定数组的嵌套类型(元素类型)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/840878/

相关文章:

c# - 如何获取winform的GUI线程?

java - Java使用反射创建泛型参数化类

Java : Implicit cast of Object at compile-time doesn't work but work with reflection

c# - 文化不变 bool.TryParse

c# - Selenium 单击与文本对应的 javascript 按钮

c# - 如何同步使用 UserManager?

c# - 更喜欢 EqualityComparer<T> 而不是 IEqualityComparer<T>

c# - 您如何确定控制台应用程序是如何启动的?

c# - 如何在没有 pdb 的情况下在堆栈跟踪中包含行号?

java - 是否有可能获得所有实现接口(interface)的类?