I want to call specific methods through delegates, but I get a VerificationException. I am using the following code:
internal delegate void Delegete_add_Startup(object o, StartupEventHandler s); Delegete_add_Startup del; public App() { //this.Startup += this.Application_Startup; Type[] parameterTypes = new Type[2]; parameterTypes[0] = typeof(object); parameterTypes[1] = typeof(StartupEventHandler); MethodInfo mi = typeof(Application).GetMethod("add_Startup", BindingFlags.Public | BindingFlags.Instance); DynamicMethod method = new DynamicMethod(string.Empty, mi.ReturnType, parameterTypes); method.InitLocals = true; ILGenerator iLGenerator = method.GetILGenerator(); iLGenerator.Emit(OpCodes.Ldarg_0); iLGenerator.Emit(OpCodes.Ldarg_1); iLGenerator.Emit(OpCodes.Call, mi); iLGenerator.Emit(OpCodes.Ret); del = (Delegete_add_Startup)method.CreateDelegate(typeof(Delegete_add_Startup)); del(this, new StartupEventHandler(Application_Startup)); this.Exit += this.Application_Exit; this.UnhandledException += this.Application_UnhandledException; InitializeComponent(); }
Basically, I'm trying to call
this.Startup + = this.Application_Startup;
through the delegate using the code above.
This gives a VerificationException: an operation can destabilize an exception at runtime.
This is very easy to reproduce by putting this code in the App constructor of the new Silverlight App project. What am I doing wrong?
source share