2009-05-08 97 views
36

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

回答

59
t.GetElementType() 

Reference

+0

優秀!謝謝! – 2009-05-08 17:16:24

+1

我希望取悅:)。 – swilliams 2009-05-08 17:17:43

相關問題