我做了一些研究,发现使用反射显然是一种简单易懂的方法。
Type MyType = typeof(MyObject);
IList lst = (IList)Activator.CreateInstance((typeof(List<>).MakeGenericType(MyType)));
我收到编译错误。它告诉我其实需要为IList提供类型... 我错过了什么吗?
任何帮助都会非常感激,欢呼。
答案 0 :(得分:4)
你忘了这个:
using System.Collections;
没有它,它只会看到System.Collections.Generic.IList<T>
,因此会抱怨丢失T
。