Redirecting to a dynamic method from a generic event handler

末鹿安然 提交于 2019-12-11 05:29:15

问题


I'm trying to write a class that's to be used to trigger a call to a method from an arbitrary event but I'm stuck as I simply cannot figure out a way to reference 'this' from emitted MSIL code.

This example should describe what I'm looking for:

class MyEventTriggeringClass
{ 
    private object _parameter;

    public void Attach(object source, string eventName, object parameter)
    {
        _parameter = parameter;
        var e = source.GetType().GetEvent(eventName);
        if (e == null) return;
        hookupDelegate(source, e);
    }

    private void hookupDelegate(object source, EventInfo e)
    {
        var handlerType = e.EventHandlerType;
        // (omitted some validation here)
        var dynamicMethod = new DynamicMethod("invoker",
                  null,
                  getDelegateParameterTypes(handlerType), // (omitted this method in this exmaple)
                  GetType());
        var ilgen = dynamicMethod.GetILGenerator();
        var toBeInvoked = GetType().GetMethod(
            "invokedMethod", 
            BindingFlags.NonPublic | BindingFlags.Instance);
        ilgen.Emit(OpCodes.Ldarg_0); // <-- here's where I thought I could push 'this' (failed)
        ilgen.Emit(OpCodes.Call, toBeInvoked);
        ilgen.Emit(OpCodes.Ret);
        var sink = dynamicMethod.CreateDelegate(handlerType);
        e.AddEventHandler(source, sink);
    }

    private void invokedMethod()
    {
        Console.WriteLine("Value of _parameter = " + _parameter ?? "(null)"); 
        // output is always "(null)"
    }
}

Here's an xample how I envision the class being used:

var handleEvent = new MyEventTriggeringClass();
handleEvent.Attach(someObject, "SomeEvent", someValueToBePassedArround);

(Please note that the above example is quite pointless. I just try to describe what I'm looking for. My final goal here is to be able to trigger a call to an arbitrary method whenever an arbitrary event fires. I'll use that in a WPF projekt where I try to use 100% MVVM but I've stumbled upon one of the [seemingly] classic breaking points.)

Anyway, the code "works" so far as it successfully invoked the "invokedMethod" when the arbitrary event fires but 'this' seems to be an empty object (_parameter is always null). I have done some research but simply cannot find any good examples where 'this' is properly passed to a method being called from within a dynamic method like this.

The closest example I've found is THIS ARTICLE but in that example 'this' can be forced to the dynamic method since it's called from the code, not an arbitrary event handler.

Any suggestions or hints would be very appreciated.


回答1:


Because of the way variance on delegates works in .Net, you can write the code in C# without using codegen:

private void InvokedMethod(object sender, EventArgs e)
{
    // whatever
}

private MethodInfo _invokedMethodInfo =
    typeof(MyEventTriggeringClass).GetMethod(
        "InvokedMethod", BindingFlags.Instance | BindingFlags.NonPublic);

private void hookupDelegate(object source, EventInfo e)
{
    Delegate invokedMethodDelegate = 
        Delegate.CreateDelegate(e.EventHandlerType, this, _invokedMethodInfo);
    e.AddEventHandler(source, invokedMethodDelegate);
}

To explain, let's say you have some event that follows the standard event pattern, that is, return type is void, first parameter is object and second parameter is EventArgs or some type derived from EventArgs. If you have that and InvokeMethod defined as above, you can write someObject.theEvent += InvokedMethod. This is allowed because it is safe: you know the second parameter is some type that can act as EventArgs.

And the code above is basically the same, except using reflection when given the event as EventInfo. Just create a delegate of the correct type that references our method and subscribe to the event.




回答2:


If you're sure you want to go with the codegen way, possibly because you want to support non-standard events too, you could do it like this:

Whenever you want to attach to an event, create a class that has a method that matches the event's delegate type. The type will also have a field that holds the passed-in parameter. (Closer to your design would be a field that holds a reference to the this instance of MyEventTriggeringClass, but I think it makes more sense this way.) This field is set in the constructor.

The method will call invokedMethod, passing parameter as a parameter. (This means invokedMethod has to be public and can be made static, if you don't have another reason to keep in non-static.)

When we're done creating the class, create an instance of it, create a delegate to the method and attach that to the event.

public class MyEventTriggeringClass
{
    private static readonly ConstructorInfo ObjectCtor =
        typeof(object).GetConstructor(Type.EmptyTypes);

    private static readonly MethodInfo ToBeInvoked =
        typeof(MyEventTriggeringClass)
            .GetMethod("InvokedMethod",
                       BindingFlags.Public | BindingFlags.Static);

    private readonly ModuleBuilder m_module;

    public MyEventTriggeringClass()
    {
        var assembly = AppDomain.CurrentDomain.DefineDynamicAssembly(
            new AssemblyName("dynamicAssembly"),
            AssemblyBuilderAccess.RunAndCollect);

        m_module = assembly.DefineDynamicModule("dynamicModule");
    }

    public void Attach(object source, string @event, object parameter)
    {
        var e = source.GetType().GetEvent(@event);
        if (e == null)
            return;
        var handlerType = e.EventHandlerType;

        var dynamicType = m_module.DefineType("DynamicType" + Guid.NewGuid());

        var thisField = dynamicType.DefineField(
            "parameter", typeof(object),
            FieldAttributes.Private | FieldAttributes.InitOnly);

        var ctor = dynamicType.DefineConstructor(
            MethodAttributes.Public, CallingConventions.HasThis,
            new[] { typeof(object) });

        var ctorIL = ctor.GetILGenerator();
        ctorIL.Emit(OpCodes.Ldarg_0);
        ctorIL.Emit(OpCodes.Call, ObjectCtor);
        ctorIL.Emit(OpCodes.Ldarg_0);
        ctorIL.Emit(OpCodes.Ldarg_1);
        ctorIL.Emit(OpCodes.Stfld, thisField);
        ctorIL.Emit(OpCodes.Ret);

        var dynamicMethod = dynamicType.DefineMethod(
            "Invoke", MethodAttributes.Public, typeof(void),
            GetDelegateParameterTypes(handlerType));

        var methodIL = dynamicMethod.GetILGenerator();
        methodIL.Emit(OpCodes.Ldarg_0);
        methodIL.Emit(OpCodes.Ldfld, thisField);
        methodIL.Emit(OpCodes.Call, ToBeInvoked);
        methodIL.Emit(OpCodes.Ret);

        var constructedType = dynamicType.CreateType();

        var constructedMethod = constructedType.GetMethod("Invoke");

        var instance = Activator.CreateInstance(
            constructedType, new[] { parameter });

        var sink = Delegate.CreateDelegate(
            handlerType, instance, constructedMethod);

        e.AddEventHandler(source, sink);
    }

    private static Type[] GetDelegateParameterTypes(Type handlerType)
    {
        return handlerType.GetMethod("Invoke")
                          .GetParameters()
                          .Select(p => p.ParameterType)
                          .ToArray();
    }

    public static void InvokedMethod(object parameter)
    {
        Console.WriteLine("Value of parameter = " + parameter ?? "(null)");
    }
}

This is still doesn't take care of all possible events, though. That's because the delegate of an event can have a return type. That would mean giving a return type to the generated method and returning some value (probably default(T)) from it.

There's (at least) one possible optimization: don't create a new type every time, but cache them. When you try to attach to an event with the same signature as a previous one, use use its class.




回答3:


I'm gonna go ahead and answer my own question here. The solution was very simple once I realized what the real problem was: Specifying the event handler's instance/target. This is done by adding an argument to MethodInfo.CreateDelegate().

If you're interested, here's a simple example you can cut'n'paste into a console app and try it out:

class Program
{
    static void Main(string[] args)
    {
        var test = new MyEventTriggeringClass();
        var eventSource = new EventSource();
        test.Attach(eventSource, "SomeEvent", "Hello World!");
        eventSource.RaiseSomeEvent();
        Console.ReadLine();
    }
}

class MyEventTriggeringClass
{
    private object _parameter;

    public void Attach(object eventSource, string eventName, object parameter)
    {
        _parameter = parameter;
        var sink = new DynamicMethod(
            "sink",
            null,
            new[] { typeof(object), typeof(object), typeof(EventArgs) },
            typeof(Program).Module);

        var eventInfo = typeof(EventSource).GetEvent("SomeEvent");

        var ilGenerator = sink.GetILGenerator();
        var targetMethod = GetType().GetMethod("TargetMethod", BindingFlags.Instance | BindingFlags.Public, null, new Type[0], null);
        ilGenerator.Emit(OpCodes.Ldarg_0); // <-- loads 'this' (when sink is not static)
        ilGenerator.Emit(OpCodes.Call, targetMethod);
        ilGenerator.Emit(OpCodes.Ret);

        // SOLUTION: pass 'this' as the delegate target...
        var handler = (EventHandler)sink.CreateDelegate(eventInfo.EventHandlerType, this);
        eventInfo.AddEventHandler(eventSource, handler);
    }

    public void TargetMethod()
    {
        Console.WriteLine("Value of _parameter = " + _parameter);
    }
}

class EventSource
{
    public event EventHandler SomeEvent;

    public void RaiseSomeEvent()
    {
        if (SomeEvent != null)
            SomeEvent(this, new EventArgs());
    }
}

So, thanks for your comments and help. Hopefully someone learned something. I know I did.

Cheers




回答4:


Here is my own version / for my own needs:

    /// <summary>
    /// Corresponds to 
    ///     control.Click += new EventHandler(method);
    /// Only done dynamically, and event arguments are omitted.
    /// </summary>
    /// <param name="objWithEvent">Where event resides</param>
    /// <param name="objWhereToRoute">To which object to perform execution to</param>
    /// <param name="methodName">Method name which to call. 
    ///  methodName must not take any parameter in and must not return any parameter. (.net 4.6 is strictly checking this)</param>
    private static void ConnectClickEvent( object objWithEvent, object objWhereToRoute, string methodName )
    {
        EventInfo eventInfo = null;

        foreach (var eventName in new String[] { "Click" /*WinForms notation*/, "ItemClick" /*DevExpress notation*/ })
        {
            eventInfo = objWithEvent.GetType().GetEvent(eventName);
            if( eventInfo != null )
                break;
        }

        Type objWhereToRouteObjType = objWhereToRoute.GetType();
        var method = eventInfo.EventHandlerType.GetMethod("Invoke");
        List<Type> types = method.GetParameters().Select(param => param.ParameterType).ToList();
        types.Insert(0, objWhereToRouteObjType);

        var methodInfo = objWhereToRouteObjType.GetMethod(methodName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, new Type[0], null);
        if( methodInfo.ReturnType != typeof(void) )
            throw new Exception("Internal error: methodName must not take any parameter in and must not return any parameter");

        var dynamicMethod = new DynamicMethod(eventInfo.EventHandlerType.Name, null, types.ToArray(), objWhereToRouteObjType);

        ILGenerator ilGenerator = dynamicMethod.GetILGenerator(256);
        ilGenerator.Emit(OpCodes.Ldarg_0);
        ilGenerator.EmitCall(OpCodes.Call, methodInfo, null);
        ilGenerator.Emit(OpCodes.Ret);

        var methodDelegate = dynamicMethod.CreateDelegate(eventInfo.EventHandlerType, objWhereToRoute);
        eventInfo.AddEventHandler(objWithEvent, methodDelegate);
    } //ConnectClickEvent


来源:https://stackoverflow.com/questions/7887200/redirecting-to-a-dynamic-method-from-a-generic-event-handler

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!