8

実行時にC#でインターフェイスを実装するクラスを生成(発行)したいと思います。

メソッドとプロパティを発行することに成功しましたが、イベントを発行できませんでした。

これが私が出したいコードです:

名前空間:TestInterfaces、クラス:TestIImplementMe。

public event EventHandler SimpleEvent;

生成されるILコードは次のとおりです。

.event [mscorlib]System.EventHandler SimpleEvent
{
    .addon instance void TestInterfaces.TestIImplementMe::add_SimpleEvent(class [mscorlib]System.EventHandler)
    .removeon instance void TestInterfaces.TestIImplementMe::remove_SimpleEvent(class [mscorlib]System.EventHandler)
}

.method public final hidebysig specialname newslot virtual 
    instance void add_SimpleEvent (
        class [mscorlib]System.EventHandler 'value'
    ) cil managed 
{
    // Method begins at RVA 0x330c
    // Code size 48 (0x30)
    .maxstack 3
    .locals init (
        [0] class [mscorlib]System.EventHandler,
        [1] class [mscorlib]System.EventHandler,
        [2] class [mscorlib]System.EventHandler,
        [3] bool
    )

    IL_0000: ldarg.0
    IL_0001: ldfld class [mscorlib]System.EventHandler TestInterfaces.TestIImplementMe::SimpleEvent
    IL_0006: stloc.0
    // loop start (head: IL_0007)
        IL_0007: ldloc.0
        IL_0008: stloc.1
        IL_0009: ldloc.1
        IL_000a: ldarg.1
        IL_000b: call class [mscorlib]System.Delegate [mscorlib]System.Delegate::Combine(class [mscorlib]System.Delegate, class [mscorlib]System.Delegate)
        IL_0010: castclass [mscorlib]System.EventHandler
        IL_0015: stloc.2
        IL_0016: ldarg.0
        IL_0017: ldflda class [mscorlib]System.EventHandler TestInterfaces.TestIImplementMe::SimpleEvent
        IL_001c: ldloc.2
        IL_001d: ldloc.1
        IL_001e: call !!0 [mscorlib]System.Threading.Interlocked::CompareExchange<class [mscorlib]System.EventHandler>(!!0&, !!0, !!0)
        IL_0023: stloc.0
        IL_0024: ldloc.0
        IL_0025: ldloc.1
        IL_0026: ceq
        IL_0028: ldc.i4.0
        IL_0029: ceq
        IL_002b: stloc.3
        IL_002c: ldloc.3
        IL_002d: brtrue.s IL_0007
    // end loop
    IL_002f: ret
} // end of method TestIImplementMe::add_SimpleEvent

.method public final hidebysig specialname newslot virtual 
    instance void remove_SimpleEvent (
        class [mscorlib]System.EventHandler 'value'
    ) cil managed 
{
    // Method begins at RVA 0x3348
    // Code size 48 (0x30)
    .maxstack 3
    .locals init (
        [0] class [mscorlib]System.EventHandler,
        [1] class [mscorlib]System.EventHandler,
        [2] class [mscorlib]System.EventHandler,
        [3] bool
    )

    IL_0000: ldarg.0
    IL_0001: ldfld class [mscorlib]System.EventHandler TestInterfaces.TestIImplementMe::SimpleEvent
    IL_0006: stloc.0
    // loop start (head: IL_0007)
        IL_0007: ldloc.0
        IL_0008: stloc.1
        IL_0009: ldloc.1
        IL_000a: ldarg.1
        IL_000b: call class [mscorlib]System.Delegate [mscorlib]System.Delegate::Remove(class [mscorlib]System.Delegate, class [mscorlib]System.Delegate)
        IL_0010: castclass [mscorlib]System.EventHandler
        IL_0015: stloc.2
        IL_0016: ldarg.0
        IL_0017: ldflda class [mscorlib]System.EventHandler TestInterfaces.TestIImplementMe::SimpleEvent
        IL_001c: ldloc.2
        IL_001d: ldloc.1
        IL_001e: call !!0 [mscorlib]System.Threading.Interlocked::CompareExchange<class [mscorlib]System.EventHandler>(!!0&, !!0, !!0)
        IL_0023: stloc.0
        IL_0024: ldloc.0
        IL_0025: ldloc.1
        IL_0026: ceq
        IL_0028: ldc.i4.0
        IL_0029: ceq
        IL_002b: stloc.3
        IL_002c: ldloc.3
        IL_002d: brtrue.s IL_0007
    // end loop
    IL_002f: ret
} // end of method TestIImplementMe::remove_SimpleEvent

この行以外はすべて実行できました:IL_001e:call !! 0 [mscorlib] System.Threading.Interlocked :: CompareExchange(!! 0&、!! 0、!! 0)

!! 0、!! 0&の意味は何ですか?

ILemitでこのメソッドを呼び出すにはどうすればよいですか?

メソッドを呼び出そうとしましたが、ハンドラーを追加すると:

obj.SimpleEvent += new EventHandler(obj_SimpleEvent);

この例外が発生します:

System.BadImageFormatException was unhandled
  Message=An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B)
  Source=DataBuilderAssembly
  StackTrace:
       at NewTest.add_SimpleEvent(EventHandler value)
       at TestInterfaces.Program.Main(String[] args) in D:\Dev\Private\TestInterfaces\TestInterfaces\Program.cs:line 47
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()

これは、イベントのaddメソッドを発行するコードです。

methodName = "add_SimpleEvent";
actionHanler = typeof(Delegate).GetMethod("Combine", new Type[]{typeof(Delegate),typeof(Delegate)});
eventField = typeBuilder.DefineField(info.Name, info.EventHandlerType, FieldAttributes.Private);
compareExchange = GetGenericMethod(typeof(Interlocked), "CompareExchange");// finds the correct method to call.
// thanks to @marc, create the specific method with the correct type
compareExchange = compareExchange.MakeGenericMethod(info.EventHandlerType);

GenerateEventMethod

private static MethodBuilder GenerateEventMethod(string methodName, MethodInfo actionHanler, TypeBuilder typeBuilder, EventInfo info, FieldBuilder eventField, MethodInfo compareExchange)
{
    MethodBuilder method = typeBuilder.DefineMethod(methodName, InternalMethodsAttributes, null, new Type[] { info.EventHandlerType });
    method.DefineParameter(0, ParameterAttributes.Retval, null);
    method.DefineParameter(1, ParameterAttributes.In, "value");
    ILGenerator il = method.GetILGenerator();
    il.DeclareLocal(info.EventHandlerType);
    il.DeclareLocal(info.EventHandlerType);
    il.DeclareLocal(info.EventHandlerType);
    il.DeclareLocal(typeof (bool));

    Label loop = il.DefineLabel();

    il.Emit(OpCodes.Ldarg_0);
    il.Emit(OpCodes.Ldfld, eventField);
    il.Emit(OpCodes.Stloc_0);

    il.MarkLabel(loop);// loop start (head: IL_0007)

    il.Emit(OpCodes.Ldloc_0);
    il.Emit(OpCodes.Stloc_1);
    il.Emit(OpCodes.Ldloc_1);
    il.Emit(OpCodes.Ldarg_1);

    il.Emit(OpCodes.Call, actionHanler);
    il.Emit(OpCodes.Castclass, info.EventHandlerType);

    il.Emit(OpCodes.Stloc_2);
    il.Emit(OpCodes.Ldarg_0);

    il.Emit(OpCodes.Ldflda, eventField);

    il.Emit(OpCodes.Ldloc_2);
    il.Emit(OpCodes.Ldloc_1);

    // How to do this?
    //IL_001e: call !!0 [mscorlib]System.Threading.Interlocked::CompareExchange<class [mscorlib]System.EventHandler`1<class [mscorlib]System.ConsoleCancelEventArgs>>(!!0&, !!0, !!0)
    //il.Emit(OpCodes.Call, !!0 compareExchange(!!0&,!!0, !!0));
    il.Emit(OpCodes.Call, compareExchange);

    il.Emit(OpCodes.Stloc_0);
    il.Emit(OpCodes.Ldloc_0);
    il.Emit(OpCodes.Ldloc_1);
    il.Emit(OpCodes.Ceq);
    il.Emit(OpCodes.Ldc_I4_0);
    il.Emit(OpCodes.Ceq);
    il.Emit(OpCodes.Stloc_3);
    il.Emit(OpCodes.Ldloc_3);
    il.Emit(OpCodes.Brtrue_S, loop);
    // end loop
    il.Emit(OpCodes.Ret);

    return method;
}

GetGenericMethod(この問題に固有):

private static MethodInfo GetGenericMethod(Type type, string methodName)
{
    var q = from m in type.GetMethods()
            where m.Name == methodName && m.IsGenericMethod
            select m;
    return q.FirstOrDefault();
}

ありがとう、Ofir

4

2 に答える 2

5

!!0Tin CompareExchange<T>(つまり!!0、最初の (0 ベースの) ジェネリック型パラメーター) であり、かつ!!0&is ref T- ですが、これらはすべてオーバーロードを指定しているだけです。重要なビットは、ldflda (フィールドを参照によってロードする)、その前の行の ldloc.2 と ldloc.1、およびそれが a を返すという事実ですT

于 2012-09-02T08:22:32.643 に答える
1

コードをC#で記述してコンパイルし、お気に入りの.NETデコンパイラーの1つを使用して、正確なILコードを確認することをお勧めします。これを行った後、同じコードを動的に生成するのは非常に簡単です。

于 2012-09-02T08:28:11.453 に答える