4

デリゲートを介して特定のメソッドを呼び出したいのですが、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アプリプロジェクトのアプリコンストラクターに配置することで、これを非常に簡単に再現できます。私は何が間違っているのですか?

4

1 に答える 1

0

あなたのケースでは、OpCodes.Call を OpCodes.CallVirt に置き換えることができます。これはより適切に機能するはずです (テストも理解もされていません。私は Silverlight CLR の機微の新人です)。

于 2011-06-15T12:38:15.633 に答える