2009-02-05 11 views

risposta

25
Type generic = typeof(List<>);  
Type specific = generic.MakeGenericType(typeof(int));  
ConstructorInfo ci = specific.GetConstructor(Type.EmptyTypes);  
object o = ci.Invoke(new object[] { }); 
+4

questo non è un grande affare o altro, ma è possibile sostituire il tipo di matrice vuota viene passata in GetConstructor con Type.EmptyTypes. Solo un po 'più pulito, tutto qui. – Kilhoffer

8

Si potrebbe utilizzare la riflessione per questo:

Type t = typeof(Car); 
System.Type genericType= generic.MakeGenericType(new System.Type[] { t}); 
Activator.CreateInstance(genericType, args);