.NET: How to pass value when subscribing to event and obtain it when the event is triggered (Dynamic

Posted by Entrase on Stack Overflow See other posts from Stack Overflow or by Entrase
Published on 2010-05-10T10:47:17Z Indexed on 2010/05/10 10:54 UTC
Read the original article Hit count: 281

The task is to create event handlers in runtime. I need the one method to be called with different parameter value for different events. The events and their number are only known in runtime. So I'm trying to generate dynamic methods, each of which will be assigned to some event, but in general they all just pass some value to an instance method and call it.

It would be great if something similar could be done the easier way. I mean passing some value at subscribing stage and then obtaining it when the event is triggered.

This is what I'm trying to do now:

public class EventSource
{
    public event EventHandler eventOne;
    public event EventHandler eventTwO;
    public event EventHandler eventThree;
}

public class EventListener
{
    SubscribeForEvents()
    {
        BindingFlags flags =
                    BindingFlags.IgnoreCase |
                    BindingFlags.Public |
                    BindingFlags.Instance;

        // Suppose we've already got EventInfo
        // and target source somewhere
        // so we can do eventInfo.AddEventHandler(target, delegate)
        // Now we need a delegate.

        int value = 42;
        Type tDelegate = eventInfo.EventHandlerType;

        // http://msdn.microsoft.com/en-us/library/ms228976(VS.95).aspx
        Type returnType = GetDelegateReturnType(tDelegate);
        DynamicMethod listener = new DynamicMethod("", null,
            GetDelegateParameterTypes(tDelegate), this.GetType());
        /////////

        Type[] callParameters = { typeof(int) };
        MethodInfo method = this.GetType().GetMethod("ToCallFromDelegate", flags);
        ILGenerator generator = listener.GetILGenerator();

        // No success in this mess. What's wrong?
        generator.Emit(OpCodes.Ldc_I4, value);
        generator.Emit(OpCodes.Call, method);
        generator.Emit(OpCodes.Pop);
        generator.Emit(OpCodes.Ret);
        /////////////

        Delegate delegate = listener.CreateDelegate(tDelegate);
        eventInfo.AddEventHandler(target, delegate);

        // When triggered, there is InvalidProgramException
    }

    void ToCallFromDelegate(int value)
    {
        doSomething();
    }
}

© Stack Overflow or respective owner

Related posts about intermediate-language

Related posts about dynamicmethod