给定简单类型T,得到数组类型T []

Cri*_*scu 1 .net arrays reflection types

我怎样才能得到Array of type T给定的类型T

LinqPad友好的片段如下:

void Main()
{
    Type t = typeof(string);
    Type tArray = GetArrayType(t);
    tArray.Dump(); // System.String[]
}
Type GetArrayType(Type t)
{
    ////this is cheating !!
    //return typeof(string[]); 
}
Run Code Online (Sandbox Code Playgroud)

Wil*_*ill 6

这样就可以了:

type.MakeArrayType()
Run Code Online (Sandbox Code Playgroud)

例如:

int a = 123;
Type aType = a.GetType();
Type aArrayType = aType.MakeArrayType();

// aArrayType.FullName = "System.Int32[]"
Run Code Online (Sandbox Code Playgroud)