Open sandboxFocusImprove this doc

LA0195: Event Interception Applied to an Interface Event

Severity Error
Reported by PostSharp
Message "<AspectOrAdvice>" cannot intercept "<Event>" because it is an instance event of an interface.

Cause

Since C# 8, an interface can give its events a default implementation with add and remove accessors. PostSharp cannot intercept the instance events of an interface: an EventInterceptionAspect, or an event advice such as OnEventAddHandlerAdvice, reports LA0195 when it targets a non-static event whose declaring type is an interface. <AspectOrAdvice> is the aspect type, or the advice method.

In the following example, the [TraceSubscriptions] aspect is applied to an event of the IOrderNotifier interface.

using System;
using PostSharp.Aspects;
using PostSharp.Serialization;

namespace Errors.LA0195_Error;

[PSerializable]
public class TraceSubscriptionsAttribute : EventInterceptionAspect
{
    public override void OnAddHandler( EventInterceptionArgs args )
    {
        Console.WriteLine( $"Subscribing to {args.Event.Name}." );
        base.OnAddHandler( args );
    }
}

public interface IOrderNotifier
{
    // The aspect cannot intercept an instance event declared in an interface.
    [TraceSubscriptions]
    event EventHandler? OrderPlaced
    {
        add => Console.WriteLine( "The default implementation ignores the handler." );
        remove { }
    }
}

public class OrderService : IOrderNotifier
{
}

public static class Program
{
    public static void Main()
    {
        IOrderNotifier notifier = new OrderService();
        notifier.OrderPlaced += ( sender, e ) => Console.WriteLine( "Order placed." );
    }
}
error LA0195: LA0195_Error.cs(21,25): "Errors.LA0195_Error.TraceSubscriptionsAttribute" cannot intercept "OrderPlaced" because it is an instance event of an interface.

Resolution

Apply the aspect to the event of the class that implements the interface, rather than to the interface.

In the following example, the interface declares the event, and the aspect intercepts the event of OrderService.

using System;
using PostSharp.Aspects;
using PostSharp.Serialization;

namespace Errors.LA0195_Fixed;

[PSerializable]
public class TraceSubscriptionsAttribute : EventInterceptionAspect
{
    public override void OnAddHandler( EventInterceptionArgs args )
    {
        Console.WriteLine( $"Subscribing to {args.Event.Name}." );
        base.OnAddHandler( args );
    }
}

public interface IOrderNotifier
{
    event EventHandler? OrderPlaced;
}

public class OrderService : IOrderNotifier
{
    // The aspect intercepts the event of the class that implements the interface.
    [TraceSubscriptions]
    public event EventHandler? OrderPlaced;

    public void PlaceOrder() => this.OrderPlaced?.Invoke( this, EventArgs.Empty );
}

public static class Program
{
    public static void Main()
    {
        var service = new OrderService();
        IOrderNotifier notifier = service;
        notifier.OrderPlaced += ( sender, e ) => Console.WriteLine( "Order placed." );
        service.PlaceOrder();
    }
}
Subscribing to OrderPlaced.
Order placed.

See Also

Reference

EventInterceptionAspect
OnEventAddHandlerAdvice

Other Resources

Intercepting Events