如何确定List<>中的尖括号之间的类型在运行时间?

时间:2016-12-01 18:19:55

标签: c#

enter image description here

大家好 我的代码片段附有,据说,当我确定尖括号之间的类型没有问题,但我想在运行时确定类型,我该如何解决这个问题?

提前致谢

1 个答案:

答案 0 :(得分:1)

如果您愿意在运行时初始化通用列表,则必须使用反射。

Type t = a.GetType();
Type listType = typeof(List<>).MakeGenericType(t);
IList list = (IList) Activator.CreateInstance(listType);
相关问题