Open sandboxFocusImprove this doc

LA0102: Ambiguous Pointcut Method

Severity Error
Reported by PostSharp
Message Cannot choose the method "<Method>" named by the MethodPointcut of the advice "<Advice>": type "<AspectType>" has more than one method of this name with the signature of a pointcut method. Rename the other methods.

Cause

A MethodPointcut names a method of the aspect class by its name only. A pointcut method is an instance method with one parameter, of type object or of the reflection type of the target of the aspect, that returns an IEnumerable<T> of object or of the reflection type of the targets of the advice (see LA0101). When the aspect class has several overloads with this name that match this signature, for instance one that takes a Type and one that takes an object, PostSharp cannot choose one and reports LA0102 on the advice method.

PostSharp then also reports LA0049, which says that the advice has no pointcut: it ignores the pointcut that it could not resolve. Fixing LA0102 removes LA0049 too.

In the following example, the aspect has two SelectMethods methods, one for a Type and one for an object.

using System;
using System.Collections.Generic;
using System.Reflection;
using PostSharp.Aspects;
using PostSharp.Aspects.Advices;
using PostSharp.Serialization;

namespace Errors.LA0102_Error;

[PSerializable]
public class LogPublicMethodsAttribute : TypeLevelAspect
{
    private IEnumerable<MethodInfo> SelectMethods( Type type )
        => type.GetMethods( BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly );

    // This overload also has the signature of a pointcut method, so PostSharp cannot choose between the two.
    private IEnumerable<MethodInfo> SelectMethods( object target ) => this.SelectMethods( (Type) target );

    [OnMethodEntryAdvice, MethodPointcut( nameof(SelectMethods) )]
    public void OnEntry( MethodExecutionArgs args )
    {
        Console.WriteLine( $"Entering {args.Method.Name}." );
    }
}

[LogPublicMethods]
public class OrderService
{
    public void PlaceOrder() => Console.WriteLine( "Order placed." );
}

public static class Program
{
    public static void Main()
    {
        new OrderService().PlaceOrder();
    }
}
error LA0102: LA0102_Error.cs(20,17): Cannot choose the method "SelectMethods" named by the MethodPointcut of the advice "Errors.LA0102_Error.LogPublicMethodsAttribute.OnEntry(PostSharp.Aspects.MethodExecutionArgs)": type "Errors.LA0102_Error.LogPublicMethodsAttribute" has more than one method of this name with the signature of a pointcut method. Rename the other methods.

Resolution

Keep one method with the name of the pointcut and rename the other overloads, or remove them.

In the following example, the helper method is renamed SelectMethodsOf.

using System;
using System.Collections.Generic;
using System.Reflection;
using PostSharp.Aspects;
using PostSharp.Aspects.Advices;
using PostSharp.Serialization;

namespace Errors.LA0102_Fixed;

[PSerializable]
public class LogPublicMethodsAttribute : TypeLevelAspect
{
    private IEnumerable<MethodInfo> SelectMethods( Type type )
        => type.GetMethods( BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly );

    // The helper method has another name, so only one method named SelectMethods remains.
    private IEnumerable<MethodInfo> SelectMethodsOf( object target ) => this.SelectMethods( (Type) target );

    [OnMethodEntryAdvice, MethodPointcut( nameof(SelectMethods) )]
    public void OnEntry( MethodExecutionArgs args )
    {
        Console.WriteLine( $"Entering {args.Method.Name}." );
    }
}

[LogPublicMethods]
public class OrderService
{
    public void PlaceOrder() => Console.WriteLine( "Order placed." );
}

public static class Program
{
    public static void Main()
    {
        new OrderService().PlaceOrder();
    }
}
Entering PlaceOrder.
Order placed.

See Also

Reference

MethodPointcut

Other Resources

Selecting Members with Pointcuts
Advice Methods