提问者:小点点

在C#中基于typename创建字典


我有一个type变量-type t=typeof(myClass);

如何使用类型变量t创建字典-Dictionary


共1个答案

匿名用户

可以将Activator.CreateInstance与泛型类型一起使用:

var genType = typeof(Dictionary<,>).MakeGeneric(typeof(string), t);
var dictionary = Activator.CreateInstance(genType);