Ho trovato this ma ho provato a utilizzarlo e non è riuscito.Utilizzo di un delegato per chiamare un costruttore
Come posso creare un oggetto utilizzando i riflessi e renderlo veloce inserendolo in un delegato?
DynamicMethod dm = new DynamicMethod("MyCtor", t, new Type[] { });
var ctor = t.GetConstructor(new Type[] { });
ILGenerator ilgen = dm.GetILGenerator();
ilgen.Emit(OpCodes.Ldarg_0);
ilgen.Emit(OpCodes.Newobj, ctor);
ilgen.Emit(OpCodes.Ret);
var d = (Func<T>)dm.CreateDelegate(t);
dm.Invoke(null, new object[] { });
prima di metterlo in un Deleage ho provato ad almeno invocarlo e quando ho fatto sopra ottengo l'errore
An unhandled exception of type 'System.Reflection.TargetInvocationException' occurred in mscorlib.dll
Ulteriori informazioni: eccezione è stata generata dalla destinazione di una chiamata.
se chiamo d() invece ottengo l'eccezione
An unhandled exception of type 'System.ArgumentException' occurred in mscorlib.dll
Additional information: Type must derive from Delegate.
Come faccio a mettere un no costruttore param in un delegato e lo chiamano?
Che problema avevi con Activator.CreateInstance? – dsolimano
dsolimano: rallentare. Sto creando migliaia di oggetti e altro ancora. –