gpt4 book ai didi

.net - 在 Silverlight 中使用 DynamicMethod 时出现 VerificationException

转载 作者:行者123 更新时间:2023-12-01 01:35:05 25 4
gpt4 key购买 nike

我想通过委托(delegate)调用某些方法,但得到了 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;

通过使用上述代码的委托(delegate)。

这给出了 VerificationException: Operation 可能会破坏运行时异常。

通过将此代码放入全新 Silverlight App 项目的 App 构造函数中,这很容易重现。
我究竟做错了什么?

最佳答案

对于您的情况,您可以将 OpCodes.Call 替换为 OpCodes.CallVirt,它应该会更好地工作(未经测试和无法理解,我是 Silverlight CLR 的新手)。

关于.net - 在 Silverlight 中使用 DynamicMethod 时出现 VerificationException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5658513/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com