Post­Sharp Documentation / Developing Custom Aspects / Developing Simple Aspects / Intercepting Events

Intercepting Events

You interact with events in three primary ways: subscribing, unsubscribing and raising them. Like methods and properties, you may find yourself needing to intercept these three interactions. How do you execute code every time that an event is subscribed to? Or raised? Or unsubscribed? PostSharp provides you with a simple mechanism to accomplish this easily.

This topic contains the following sections:

Intercepting Add and Remove

Throughout the life of an event, it is possible to have many different event handlers subscribe and unsubscribe. You may want to log each of these actions.

  1. Add a reference to the PostSharp package to your project.

  2. Create an aspect that inherits from EventInterceptionAspect. Add the [PSerializableAttribute] custom attribute.

  3. Override the OnAddHandler(EventInterceptionArgs) method and add your logging code to the method body.

  4. Add the base.OnAddHandler call to the body of the OnAddHandler(EventInterceptionArgs) method. If this is omitted, the original call to add a handler will not be executed. Unless you want to stop the addition of the handler, you will need to add this line of code.

    C#
    [PSerializable]
    public class CustomEventHandling : EventInterceptionAspect
    {
        public override void OnAddHandler(EventInterceptionArgs args)
        {
            base.OnAddHandler(args);
            Console.WriteLine("A handler was added");
        }
    }
  5. To log the removal of an event handler, override the OnRemoveHandler(EventInterceptionArgs) method.

  6. Add the logging you require to the method body.

  7. Add the base.OnRemoveHandler call to the body of the OnRemoveHandler(EventInterceptionArgs) method. Like you saw when overriding the OnAddHandler(EventInterceptionArgs) method, if you omit this call, the original call to remove the handler will not occur.

    C#
    public override void OnRemoveHandler(EventInterceptionArgs args) 
    { 
        base.OnRemoveHandler(args); 
        Console.WriteLine("A handler was removed"); 
    }

Once you have defined the interception points in the aspect, you will need to attach the aspect to the target code. The simplest way to do this is to add the attribute to the event handler definition.

C#
public class Example 
{
    [CustomEventHandling]
    public EventHandler<EventArgs> SomeEvent; 

    public void DoSomething() 
    { 
        if (SomeEvent != null) 
        { 
            SomeEvent.Invoke(this, EventArgs.Empty); 
        } 
    } 
}
Intercepting Raise

When you are intercepting events, you may also have situations where you want to execute additional code when the event is raised. Raising of an event can occur in many places and you will want to centralize this code to avoid repetition.

  1. Override the OnInvokeHandler(EventInterceptionArgs) method in your aspect class and add the logging you require to the method body.

  2. Add a call to base.OnInvokeHandler to ensure that the original invocation occurs.

    C#
    public override void OnInvokeHandler(EventInterceptionArgs args) 
    { 
        base.OnInvokeHandler(args); 
        Console.WriteLine("A handler was invoked"); 
    }

By adding the attribute to the target event handler earlier in this process you have enabled intercepting of each raised event.

Accessing the current context

At any time, the Handler property is set to the delegate being added, removed, or invoked. You can read and write this property. If you write it, the delegate you assign must be compatible with the type of the event. The Event property gets you the EventInfo of the event being accessed.

Within OnInvokeHandler(EventInterceptionArgs), the property Arguments gives access to the arguments with which the delegate was invoked.

These concepts will be illustrated in the following example.

Example: Removing offending event subscribers

When events are subscribed to, the component that raises the event has no way to ensure that the subscriber will behave properly when that event is raised. It's possible that the subscribing code will throw an exception when the event is raised and when that happens you may want to unsubscribe the handler to ensure that it doesn't continue to throw the exception. The EventInterceptionAspect can help you to accomplish this easily.

  1. Override the OnInvokeHandler(EventInterceptionArgs) method in your aspect.

  2. In the method body add a try...catch block.

  3. In the try block add a call to base.OnInvokeHandler and in the catch block add a call to RemoveHandler(Delegate)

    C#
    [PSerializable]
    public class CustomEventHandling : EventInterceptionAspect 
    { 
        public override void OnInvokeHandler(EventInterceptionArgs args) 
        { 
            try 
            { 
                base.OnInvokeHandler(args); 
            } 
            catch (Exception e) 
            { 
                Console.WriteLine("Handler '{0}' invoked with arguments {1} failed with exception {2}.", 
                                  args.Handler.Method,  
                                  string.Join(", ", args.Arguments.Select(a => a == null ? "null" : a.ToString())), 
                                  e.GetType().Name); 
    
                args.RemoveHandler(args.Handler);  
                throw; 
            } 
        } 
    
    }

Now, any time an exception is thrown during event execution, the offending event handler will be unsubscribed from the event.

See Also