イベント EventConsumed とメソッド OnEventConsumed を次のように定義する EventConsumer というクラスがあります。
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 は何ですか?