是否有可能实现以下代码?我知道这不起作用,但我想知道是否有变通方法?
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);
A cleaner way might be to use a generic method. Do something like this:
static void AddType<T>() where T : DataObject { Indexes.Add(typeof(T), new Dictionary<int, T>()); }