4

我有一个名为 EventConsumer 的类,它定义了一个事件 EventConsumed 和一个方法 OnEventConsumed,如下所示:

public event EventHandler EventConsumed;

public virtual void OnEventConsumed(object sender, EventArgs e)
{
    if (EventConsumed != null)
        EventConsumed(this, e);
}

我需要在 OnEventConsumed 运行时添加属性,所以我使用 System.Reflection.Emit 生成一个子类。我想要的是与此等效的 MSIL:

public override void OnEventConsumed(object sender, EventArgs e)
{
    base.OnEventConsumed(sender, e);
}

我到目前为止是这样的:

...

MethodInfo baseMethod = typeof(EventConsumer).GetMethod("OnEventConsumed");
MethodBuilder methodBuilder = typeBuilder.DefineMethod("OnEventConsumed",
                                                       baseMethod.Attributes,
                                                       baseMethod.CallingConvention,
                                                       typeof(void),
                                                       new Type[] {typeof(object),
                                                                   typeof(EventArgs)});

ILGenerator ilGenerator = methodBuilder.GetILGenerator();

// load the first two args onto the stack
ilGenerator.Emit(OpCodes.Ldarg_1);
ilGenerator.Emit(OpCodes.Ldarg_2);

// call the base method
ilGenerator.EmitCall(OpCodes.Callvirt, baseMethod, new Type[0] );

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

...

我创建类型,创建类型的实例,并调用它的 OnEventConsumed 函数,然后我得到:

Common Language Runtime detected an invalid program.

...这并不完全有帮助。我究竟做错了什么?调用基类的事件处理程序的正确 MSIL 是什么?

4

3 回答 3

6

这是来自示例应用程序的 IL:


.method public hidebysig virtual instance void OnEventConsumed(object sender, class [mscorlib]System.EventArgs e) cil managed
    {
        .maxstack 8
        L_0000: nop 
        L_0001: ldarg.0 
        L_0002: ldarg.1 
        L_0003: ldarg.2 
        L_0004: call instance void SubclassSpike.BaseClass::OnEventConsumed(object, class [mscorlib]System.EventArgs)
        L_0009: nop 
        L_000a: ret 
    }

所以我认为你没有加载实例,因为你没有做 ldarg.0

于 2008-11-06T14:17:23.190 回答
1

我实际上非常接近 - 问题是我没有加载“this”参数,并且 Callvirt 调用了我真正想要调用的子类方法。因此该部分变为:

// load 'this' and the first two args onto the stack
ilGenerator.Emit(OpCodes.Ldarg_0);
ilGenerator.Emit(OpCodes.Ldarg_1);
ilGenerator.Emit(OpCodes.Ldarg_2);

// call the base method
ilGenerator.EmitCall(OpCodes.Call, baseMethod, new Type[0] );

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

现在它工作正常。

于 2008-11-06T14:16:26.827 回答
0

使用

public virtual void OnEventConsumed(object sender, EventArgs e)
{
    if (EventConsumed != null)
        EventConsumed(this, e);
}

应该

public virtual void OnEventConsumed(EventArgs e)
{
    EventHandler handler = this.EventConsumed;
    if ( null != handler ) handler( this, e );
}

.

我认为,您应该使用ILGenerator.EmitCalli并且您应该传递一种返回值类型(在这种情况下我认为 null )并传递参数类型 - 我认为“new Type[]{ typeof(EventArgs)}

于 2008-11-06T14:11:09.067 回答