关于 T[] 的反射问题

来源:互联网 发布:dnf账号数据异常解除 编辑:程序博客网 时间:2024/05/16 05:51

1. T[] 类型不适应以下代码

 Dictionary<string, Test> d = new Dictionary<string, Test>();        // Get a Type object representing the constructed type.         //        Type constructed = d.GetType();        DisplayTypeInfo(constructed);        Type generic = constructed.GetGenericTypeDefinition();

http://msdn.microsoft.com/en-us/library/system.type.getgenerictypedefinition.aspx


如果我们使用 typeof(int[]).GetGenericTypeDefinition 将被抛出一个异常,表明T[]不是一个泛型,无法获得类似 typeof(T[]) 的泛型Type



2.  反射T[] 应当使用 typeof(int).MakeArrayType

     var arr = Activator.CreateInstance(tpArray,10) as Array;

0 0
原创粉丝点击