Baby Stepping into MSIL – creating an "Event Recorder" using a DynamicMethod and Reflection.Emit

I’ve often been caught between two opposing forces – the desire to find some excuse to use Reflection.Emit to dynamically generate behavior, because it has a sort of magical quality about it, and on the other hand being completely intimidated by the strange and foreboding mysteries of IL. I finally found an excuse compelling enough to brave my fears and stick my toe in the water, and it wasn’t too bad.

The excuse to take the plunge was, as is often the case, laziness (or perhaps impatience). I had an interface with a blue million events on it, and I wanted create some unit tests that ensured all my little events were fired the right number of times, in the right order, with the right arguments, but I didn’t want to subscribe to all the events individually and set a bunch of flags, etc. etc. (BTW – I imagine this problem has been solved by superior minds – I didn’t spend much time looking)

[UPDATE] Here is a CodeProject article explaining how to do essentially the same thing as EventRecorder without using Reflection.Emit. Instead, an EventProxy intermediary class is used to store the EventName. 

The sample code, along with unit tests and a Windows Forms app demonstrating possible usage, can be found here.

Here is a typical usage:

public void Recorder_Will_Record_Events()
    var source = new TestEventSource();
    using (var recorder = new EventRecorder(source))

      Assert.That(recorder.EventHistory.Length, Is.EqualTo(5));

      Assert.That(((TestEventArgs) recorder.EventHistory[2].Args).LoveMe, Is.EqualTo("MyTestText"));
      Assert.That(((TestEventArgs) recorder.EventHistory[4].Args).LoveMe, Is.EqualTo("NewTestText"));

      Assert.That(recorder.GetInvocationsFor("SimpleEvent").Length, Is.EqualTo(3));
      Assert.That(recorder.GetInvocationsFor("EventWithCustomargs").Length, Is.EqualTo(2));

What I wanted to do was “record” all the events that were fired on a particular object during the course of some transaction, and then test that all the events that fired were the ones I was expecting, and in the appropriate order. I figured I would achieve this by creating a class called EventRecorder that could take in its constructor any other non-null object. It would then loop through each event in the objects type, subscribe to that event, and when the event fired add the name of the event and the EventArgs to a collection I could examine later. Pretty simple.

I though this would be a simple matter of using some plain ol’ reflection, like this:

 foreach (EventInfo evt in eventSource.GetType().GetEvents())
            new EventHandler(


But alas, AddEventHandler expects a delegate that exactly matches the signature of the event, meaning if I have an event defined with EventHandler<CancelEventArgs>, it isn’t going to let me pass a plain EventHandler delegate. So what to do? Dynamically generate an event handler with the appropriate signature, of course, and emit a simple call to my RecordMethod. Essentially, we’ll be generating the equivalent of this:

private void HandleEvent(object sender,EventArgs<CancelEventArgs> args)

I found an MSDN article that looked like just the ticket. The technique presented uses the DynamicMethod class, which you can use to create an ad hoc method and fill it full of op-codes to tell it what to do. In this case, we wanted to instantiate a DynamicMethod that matches the signature of the event handler we want to subscribe to. Then we simply needed to emit the IL to call the RecordEvent method and pass in the appropriate arguments. In the code below, EventSource refers to the object whose events we’re subscribing to, and was passed in to the constructor of the EventRecorder class.

/// <summary>
/// This method crates a DynamicMethod matching the signature of
/// the delegate required by the passed in EventInfo. The method
/// will simply pass the arguments on to the RecordEvent method.
/// </summary>
/// <param name="evt"></param>
protected void BindToEvent(EventInfo evt)
    Type handlerType = evt.EventHandlerType;

    if (handlerType == null)
        throw new ArgumentException("No handler could be " + 
                              " identified for " + evt.Name);

    Type[] handlerParams = GetDelegateParameterTypes(handlerType);

    //Not technically required, but may be helpful if an exception is thrown
    String dynamicHandlerName = 
        String.Format("EventSource_{0}", evt.Name);

    var handlerDef = new DynamicMethod(dynamicHandlerName,
                                       typeof (EventRecorder));

    MethodInfo recordEvent =
        typeof (EventRecorder).GetMethod(
             BindingFlags.Instance | BindingFlags.NonPublic);

    ILGenerator ilgen = handlerDef.GetILGenerator();
    //load 'this' onto the stack

    //load the second argument (EventArgs) onto the stack
    //We're not using the sender parameterf of the delegate,
    //so we skip emitting OpCodes.Ldarg_1

    //Load the name of the event onto the stack
    ilgen.Emit(OpCodes.Ldstr, evt.Name);

    //Call the RecordEvent method, passing the two loaded argumetns
    //EventArgs args and String name
    ilgen.Emit(OpCodes.Callvirt, recordEvent);

    //Return control

    //Create a delegate out of this new handler, binding to the current
    //instance as the target
    Delegate handler = handlerDef.CreateDelegate(handlerType,this);

        //Register the new delegeate with the event on the event source
        evt.AddEventHandler(EventSource, handler);
    catch (Exception ex)
        //Some COM objects don't like this method. In production, you wouldn't
        //eat this error like this, but for my example, I wanted to show events
        //on the WebBrowser control, and certain events of WebBrowser
        //don't appreciate this dynamic approach. So...

    //record this handler so we can get to it later
    //when we need to remove our registration from 
    //the event source during dispose
    _handlers.Add(evt.Name, new EventRef(handler,evt));

/// <summary>
/// Extract the signature of the event's delegate
/// </summary>
/// <param name="d"></param>
/// <returns></returns>
private static Type[] GetDelegateParameterTypes(Type d)
    if (d.BaseType != typeof (MulticastDelegate))
        throw new ApplicationException(
            d.Name + " is not a delegate.");

    MethodInfo invoke = d.GetMethod("Invoke");
    ParameterInfo[] parameters = invoke.GetParameters();
    //Allocate an extra slot, see below for why
    var typeParameters = new Type[parameters.Length+1];
    for (int i = 0; i < parameters.Length; i++)
        typeParameters[i+1] = parameters[i].ParameterType;

    //This is an extremely important step - we need to inject the type
    //of EventRecorder because we will be using EventRecorder as the Target
    //of the dynamic delegate that we create using the DynamicMethod. When
    //you go to convert your emitted MSIL to an actual delegate using DynamicMethod.CreateDelegate,
    //the first argument of the signature MUST match the target object you are binding
    //the delegate to. 
    typeParameters[0] = typeof (EventRecorder);

    return typeParameters;

/// <summary>
/// The internal method that gets called by the dynamically
/// generated delegates we are using to bind to the EventSource's
/// events. This method will get called once for every event that fires
/// on the EventSource.
/// </summary>
/// <param name="args"></param>
/// <param name="eventName"></param>
protected virtual void RecordEvent(EventArgs args, string eventName)
    EventInfo evt = _handlers[eventName].Event;

    if (evt != null)
        var history = new EventInvocation { Args = args, Event = evt };


IL is pretty close to the machine, relative to C#, so it’s entirely stack based. You load arguments onto the call stack, then perform an operation, such as calling a method. If you call a method, and the method has a signature with arguments, the arguments for the method signature are popped off the stack and consumed.

In this example we’re generating a dynamic method with a signature of two visible arguments – (object,MyEventArgs) – but we’re calling a method (RecordEvent) with a slightly different signature (EventArgs,string). We can discard the “object” argument of the dynamic method, which we do simply by not loading it onto the stack, which is why we don’t emit the opcode Ldarg_1, but we need to load the EventArgs and a string.

Looking at my source code it doesn’t quite add up though, because you can see I’m loading argument 0 (Ldarg_0) which looks like it should be “object” based on the dynamic method signature and argument 2 (Ldarg_2) which doesn’t even seem like it should exist, as the signature is (object,EventArgs) – only two arguments. Well, it turns out that all instance methods require a 0 position argument that is “this” – this argument is inserted by the compiler so you never see it, but when you say “this.DoSomething()” in your code, in IL method signature that gets created is DoSomething(MyType this). That means our call to “RecordEvent(EventArgs,string)” really looks like this: “RecordEvent(EventRecorder,EventArgs,string)”, which means we need to load 3 variables onto the stack, which we do:

Ldarg_0 = this

//Ldarg_1 = object – not needed

Ldarg_2 = EventArgs

Ldsstr = EventInfo.Name

NOT calling Ldarg_1 discards the object part of the dynamic method signature, which is OK, because that just represents EventSource anyway, and we have a reference to that already.

One important thing to note, though, is that when calling CreateDelegate on DynamicMethod it is critical to pass in “this” as the target – otherwise when the method you generated tries to access member variables, you’ll get illegal memory access errors. Also subtle, if you are creating a dynamic method that you intend to bind to an instance of an object, then the DynamicMethod needs to be created using the secret, full signature of MyMethod(EventRecorder,object,EventArgs) NOT just MyMethod(object,EventArgs) as you would expect. You can see this being taken care of in the GetDelegateParameterTypes method. This piece of info was not in the MSDN sample, and was a just a joy to figure out 🙂

By the way – a great way to figure out just what IL you need is to write the method you are hoping to generate, compile it, then view the IL in ILDASM.exe. Then you can either blindly translate the IL into calls, or you try to understand why it is doing what it is doing and modify according to your needs, but I probably never would have figured out the Ldarg_0 / Ldarg_2 mystery on my own – ILDASM saved my butt.

I need to find a better way of debugging this stuff, but the fact of the matters is that once you see what is going on, IL isn’t really any more complicated than anything else, it is just far more verbose and cryptic when you get it wrong 🙂

The rest of EventRecorder is nothing special. The sample includes unit tests and a simple windows forms example.

Here are some resources:

DynamicMethod.CreateDelegate Method

How to hook up a delegate using reflection

Reflection.Emit tidbits

Introduction to creating types with Reflection.Emit, DymamicMethod

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: