Open sandboxFocusImprove this doc

LA0251: MatchPointcut Used by an Aspect Not Applied to a Type

Severity Error
Reported by PostSharp
Message Cannot use the MatchPointcut of the advice "<Advice>" on "<Target>". MatchPointcut selects methods of the target type of the aspect, so the aspect must be applied to a type.

Cause

The MatchPointcut pointcut selects the methods with a given name that the target type of the aspect declares. It therefore requires an aspect applied to a type, such as a TypeLevelAspect. When the aspect is applied to another declaration, for instance a method with a MethodLevelAspect or the assembly with an AssemblyLevelAspect, PostSharp reports LA0251 on that declaration.

<Advice> is the advice method, with its parameter types, and <Target> is the declaration to which the aspect is applied.

In the following example, the [TraceOrders] aspect derives from MethodLevelAspect and is applied to the PlaceOrder method.

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

namespace Errors.LA0251_Error;

[PSerializable]
public class TraceOrdersAttribute : MethodLevelAspect
{
    [OnMethodEntryAdvice, MatchPointcut( "PlaceOrder" )]
    public void OnEntry( [Argument( 0 )] int quantity ) => Console.WriteLine( $"Placing an order of {quantity} items." );
}

public class OrderService
{
    // The aspect, whose advice uses MatchPointcut, is applied to a method.
    [TraceOrders]
    public void PlaceOrder( int quantity ) => Console.WriteLine( "Order placed." );
}

public static class Program
{
    public static void Main()
    {
        new OrderService().PlaceOrder( 3 );
    }
}
error LA0251: LA0251_Error.cs(19,17): Cannot use the MatchPointcut of the advice "Errors.LA0251_Error.TraceOrdersAttribute.OnEntry(System.Int32)" on "Errors.LA0251_Error.OrderService.PlaceOrder(System.Int32)". MatchPointcut selects methods of the target type of the aspect, so the aspect must be applied to a type.

Resolution

  • Derive the aspect from TypeLevelAspect, or from another aspect class applied to types, and apply it to the type that declares the methods.
  • In an aspect applied to a method, remove the pointcut: an advice without pointcut applies to the target of the aspect.
  • In an assembly-level aspect, use a MethodPointcut, or add type-level aspects with IAspectProvider.

In the following example, the aspect derives from TypeLevelAspect and is applied to the OrderService class.

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

namespace Errors.LA0251_Fixed;

[PSerializable]
public class TraceOrdersAttribute : TypeLevelAspect
{
    [OnMethodEntryAdvice, MatchPointcut( "PlaceOrder" )]
    public void OnEntry( [Argument( 0 )] int quantity ) => Console.WriteLine( $"Placing an order of {quantity} items." );
}

// The type-level aspect is applied to the class, and MatchPointcut selects its PlaceOrder method.
[TraceOrders]
public class OrderService
{
    public void PlaceOrder( int quantity ) => Console.WriteLine( "Order placed." );
}

public static class Program
{
    public static void Main()
    {
        new OrderService().PlaceOrder( 3 );
    }
}
Placing an order of 3 items.
Order placed.

See Also

Reference

MatchPointcut
TypeLevelAspect

Other Resources

Selecting Members with Pointcuts