【发布时间】:2011-10-25 03:42:29
【问题描述】:
我有一个泛型包装类,它有时可以接收一个数组类型作为其泛型。我可以通过 IsArray 属性判断提供的泛型类型是否为数组。但是有没有办法可以在代码中获取数组元素的类型?我查看了 Type 对象的所有公开属性,但没有看到它。
例子:
public class wrap<T>
{
public void doSomething()
{
if (typeof(T).IsArray)
Type arrayElementType = typeof(T).??? ;
}
}
// typeof(T) when an array is "int[]"
// typeof(T).BaseType is "System.Array"
// how to get "int" out of this?
【问题讨论】: