创建一个通用&LT; T&GT;包含类型的变量类型实例 [英] Creating a Generic<T> type instance with a variable containing the Type
本文介绍了创建一个通用&LT; T&GT;包含类型的变量类型实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否有可能实现以下code?我知道这是行不通的,但我不知道是否有解决方法吗?
K型= typeof运算(双);
清单&LT; K&GT; LST =新的List&LT; K&GT;();
解决方案
是的,有:
VAR genericListType = typeof运算(名单&LT;&GT;);
VAR specificListType = genericListType.MakeGenericType(typeof运算(双));
VAR列表= Activator.CreateInstance(specificListType);
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>();
解决方案
Yes, there is:
var genericListType = typeof(List<>);
var specificListType = genericListType.MakeGenericType(typeof(double));
var list = Activator.CreateInstance(specificListType);
这篇关于创建一个通用&LT; T&GT;包含类型的变量类型实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文