- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
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.0
和 ldarg.1
而不是 ldarg.1
和 ldarg.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/
我正在尝试纠正一个 Func,我可以用它来访问属性的 get 方法,但遇到了绊脚石。 下面的动态方法创建得很好,但是当它被调用时我得到以下错误。 VerificationException,操作可能会
在我的 App i 中调试时有这个异常:{"Operation could destabilize the runtime."} 在 foreach 循环中: foreach (var item in
我需要在运行时使用 TypeBuilder 创建一个类型。这种类型应该实现一个特定的接口(interface),以便可以在编译时统一处理这种动态类型的实例。 该接口(interface)应返回一个对象
运行 RDLC 时,我在报告框架内显示以下消息: Failed to load host assembly. Operation could destabilize the runtime. 有谁知道
是否可以在 UnityContainer 中配置的另一种类型的构造函数中实例化在 UnityContainer 中配置的类型?使用我当前的解决方案,我得到了一个 ResolutionFailedExc
System.Security.VerificationException: Operation could destabilize the runtime. at Connance.Communic
我创建了一个非常简单的函数来执行以下操作: public static object[] ToArray(int ID) { return new object[4];
我目前正在玩反射,我的短代码有问题: public class Test { public Test() { } public string Call() {
我目前正在玩反射,我的短代码有问题: public class Test { public Test() { } public string Call() {
在我的程序中,我手工制作了我的 LINQ 表达式(我正在将我自己的表达式树转换为 LINQ - 我们目前使用 EF,但这可能会改变,所以我通过使用我自己的表达式来验证应用程序的这一部分并编写一些将其转
Server Error in '/' Application. 什么是解决这个问题的好方法?我让调试单步执行 global.asax 中的所有内容,并且那里没有错误。 Operation could
我们最近升级了一个 web/mvc 应用程序以使用 StrucutreMap 3.0.4 现在,当尝试在“Line Level Timings, All Methods with Source”或更高
我最近尝试将一个由 SubSonic 2.2 生成 DAL 的 .net 2.0 项目升级到 Visual Studio 2010 下的 .NET 4.0。 项目转换没有错误,但现在我在尝试启动它时收
这段代码: namespace ConsoleApplication3 { class Program { static void Main(string[] args
考虑以下代码: protected override IEnumerable GetListInternal( IQueryModel2 queryModel) { /// Cause
我是一名优秀的程序员,十分优秀!