在 Silverlight 中使用 DynamicMethod 时出现 VerificationException
我想通过委托调用某些方法,但收到 VerificationException。我正在使用以下代码:
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();
}
基本上,我试图调用
this.Startup += this.Application_Startup;
通过使用上面代码的委托。
这会产生 VerificationException: 操作可能会破坏运行时异常的稳定性。
通过将此代码放入全新 Silverlight 应用程序项目的应用程序构造函数中,可以很容易地重现这一点。 我做错了什么?
I want to call certain methods via delegates but am getting VerificationException. I am using 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 am trying to call
this.Startup += this.Application_Startup;
via a delegate using the code above.
This gives a VerificationException: Operation could destabilize the runtime exception.
This is very easy to reproduce by putting this code in the App constructor of a brand new Silverlight App project.
What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于您的情况,您可以用 OpCodes.CallVirt 替换 OpCodes.Call,它应该工作得更好(未经测试和不理解,我是 Silverlight CLR 微妙之处的菜鸟)。
For your case, you may replace OpCodes.Call by OpCodes.CallVirt, it should work better (untested and ununderstood, I'm a rookie at Silverlight CLR subtleties).