Here is a simple example:
public static object DynamicallyCreateGeneric(Type GenericTypeSource, Type SpecificTypeSource) { System.Type SpecificType = GenericTypeSource.MakeGenericType( new System.Type[] { SpecificTypeSource } ); return Activator.CreateInstance(SpecificType); }
... then for example:
string type = "System.String"; Type t = Type.GetType(type); var DynamicallyCreatedGeneric = DynamicallyCreateGeneric(typeof(List<>), t); System.Diagnostics.Debugger.Break();
Adapt to suit your implementation and try. Of course, this method is not ideal. One of the best parts of generics is type compiler level type security.
source share