【发布时间】:2013-10-06 20:49:17
【问题描述】:
我正在通过反射实例化一维 Int32 数组的 List<T>。当我使用以下方法实例化列表时:
Type typeInt = typeof(System.Int32);
Type typeIntArray = typeInt.MakeArrayType(1);
Type typeListGeneric = typeof(System.Collections.Generic.List<>);
Type typeList = typeListGeneric.MakeGenericType(new Type[] { typeIntArray, });
object instance = typeList.GetConstructor(Type.EmptyTypes).Invoke(null);
我在列表本身上看到了这种奇怪的行为:
如果我通过反射与它交互,它似乎表现正常,但是如果我尝试将它转换为它的实际类型:
List<int[]> list = (List<int[]>)instance;
我得到了这个例外:
无法将“System.Collections.Generic.List`1[System.Int32[*]]”类型的对象转换为“System.Collections.Generic.List`1[System.Int32[]]”类型。
任何想法可能导致此问题或如何解决?我在 .net 4.0 上的 Visual Studio 2010 Express 中工作。
【问题讨论】:
标签: c# list generics reflection