首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Silverlight中使用DynamicMethod时的VerificationException

在Silverlight中使用DynamicMethod时的VerificationException
EN

Stack Overflow用户
提问于 2011-04-14 12:25:01
回答 1查看 393关注 0票数 4

我想通过委托调用某些方法,但得到了VerificationException。我使用了以下代码:

代码语言:javascript
复制
    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应用程序项目的App构造函数中,可以非常容易地重现此代码。我做错了什么?

EN

回答 1

Stack Overflow用户

发布于 2011-06-15 20:38:16

对于你的情况,你可以用OpCodes.CallVirt代替OpCodes.Call,它应该工作得更好(未经测试和理解,我是Silverlight CLR subtleties的新手)。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5658513

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档