创建一个通用的 T带有包含类型的变量的类型实例
问题描述:
有没有可能实现下面的代码?我知道它不起作用,但我想知道是否有解决方法?
Is it possible to achieve the following code? I know it doesn't work, but I'm wondering if there is a workaround?
Type k = typeof(double);
List<k> lst = new List<k>();
答
是的,有:
var genericListType = typeof(List<>);
var specificListType = genericListType.MakeGenericType(typeof(double));
var list = Activator.CreateInstance(specificListType);