Given the Type type
parameter, How can I create the dictionary: Dictionary<Type, DataCollection<T>>
, where where typeof(T) == type
?
I tried Dictionary<Type, DataCollection<Type>>
, but it doesn't work because DataCollection<T>
accepts data of type T
(where typeof(T) == type
), not of type Type
.
Also, I cannot make it generic: Dictionary<T, DataCollection<T>>
because I want all types to be accepted, not just a single type T
You can always create the type with this:
Type type = typeof(int);
Type typeDC = typeof(DataCollection<>);
var t = typeof(Dictionary<,>);
var x = t.MakeGenericType(type, typeDC.MakeGenericType(type));
object o = Activator.CreateInstance(x);
You would then need to use reflection to call the methods of o
or to pass o
to a strongly-typed method.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.