gpt4 book ai didi

c# - 动态 IL 方法导致 "Operation could destabilize the runtime"

转载 作者:太空狗 更新时间:2023-10-30 01:26:17 24 4
gpt4 key购买 nike

System.Security.VerificationException: Operation could destabilize the runtime. at Connance.CommunicatorApi.ReportApiClient.AcknowledgeRecallsAsyncDynamicHandler(Object , AcknowledgeRecallsCompletedEventArgs )

这就是我遇到的错误。我正在尝试做的(背景)是为一类方法创建一个全局事件处理程序。我在 WCF 中使用静态代理,我需要创建一个层来跟踪所有调用并返回到所有 WCF Web 方法。不幸的是,WCF 对“已完成”事件的 EventArgs 进行强类型化,这几乎不可能实现。

我决定尝试一下。如果事件是 EventHandler<SomeSpecificEventArgs> ,我还能注册一个签名方法void Method(object, object)来处理事件。伟大的。所以我开始创建一个 DynamicMethod这会调用我的global 处理程序,并将其注册到每个事件。

我尝试了两种方式:

1) DynamicMethod is of type void (object, object)

2) of type void (object, SomeSpecificEventArgs) -- I use a generic method for this to get the type.

只有当我尝试手动或为事件调用该方法时,才会出现上述异常。

这是我的代码:

    // The handler for all callbacks.
// in the example it does nothing.
public void Handler(object sender, object e)
{
dynamic evtArgs = e;
object userState = evtArgs.UserState;
}

private string GetIdentifier(Delegate d)
{
return string.Concat(d.Method.DeclaringType, '.', d.Method.Name);
}

// Method to register an event handler
public void Register<T> (Delegate o) where T : EventArgs
{
// get some info
/* snip. code to get method name, and calculate name of event */

var eventInst = ownerType.GetEvent(eventName);

// The following works, for example:
// someObj.MethodCompleted += Handler;
// even though MethodCompleted is an event of type EventHandler<SomeSpecialEventArgs>

// get the actual type of handler
var handlerType = eventInst.EventHandlerType;
EventHandler evtHandler = new EventHandler(Handler);

DynamicMethod dm = new DynamicMethod(
GetIdentifier(o) + "DynamicHandler", // set the name
typeof(void), // return void
new[] { typeof(object), typeof(T) });// params object and type of event args

ILGenerator gen = dm.GetILGenerator();

gen.Emit(OpCodes.Ldarg_0); // load first arg to stack for calling
gen.Emit(OpCodes.Ldarg_2); // load second arg to stack for calling

gen.Emit(OpCodes.Call, evtHandler.Method); // call method

gen.Emit(OpCodes.Ret); // return

// this is the final delegate
var superdlg = dm.CreateDelegate(handlerType);

// the problem beings here:
// when the event is raised and the delegate is invoked
// of if I dynamicInvoke it, I get the error
eventInst.AddEventHandler(ownerInst, superdlg);
}

编辑:我懂了。原来我还有一个问题。我在 Silverlight 工作。我设法在一个单独的项目中重现了我的场景,并通过使用 DynamicMethod 的重载使其工作。这允许您设置所有者。然后我指定

DynamicMethod dm = new DynamicMethod("TestMethod2", typeof(void), new[] { typeof(MyClass), typeof(string), typeof(string) }, typeof(MyClass)); ,

并使用ldarg.0 , ldarg.1 , 和 ldarg.2 .但这是一个安全关键的构造函数,不会在 silverlight 上运行。我只是不确定我需要如何设置它。我做 Handler 吗?公共(public)静态和加载参数 0-1?我最终得到这样的错误:

Attempt by method 'DynamicClass.TestMethod2(System.String, System.String)' to access method 'dynamicass.MyClass.Handler(System.String, System.String)' failed."}

最佳答案

方法参数是零索引 - 使用 ldarg.0ldarg.1 而不是 ldarg.1ldarg.2

调用事件处理程序方法也存在问题 - 您没有为方法 (Delegate.Target) 指定 this 指针。您需要提供一个 this 指针,它可能是也可能不是静态的,具体取决于注册的内容。

这也不会处理多播委托(delegate) - 这只会调用在事件上注册的处理程序之一。您需要做的是生成类似这样的方法:

.method public static CallEventHandler(EventHandlerType ev, object sender, EventArgsType e) {
ldarg.0 // the Invoke 'this' pointer
ldarg.1
ldarg.2
callvirt instance void EventHandlerType::Invoke(object, EventArgsType)
ret
}

这使用事件的 Invoke 方法,该方法处理为您调用所有已注册的处理程序。

关于c# - 动态 IL 方法导致 "Operation could destabilize the runtime",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5324871/

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