Open sandboxFocusImprove this doc

LA0085: Invalid Signature of an Advice Method

Severity Error
Reported by PostSharp
Message Advice "<Advice>" is not valid on method "<Method>": the method does not have the signature that this advice requires.

Cause

Each kind of advice requires a signature of the advice method. PostSharp reports LA0085 in the following cases:

<Advice> is the type of the advice attribute. Depending on the kind of advice, PostSharp reports the error on the advice method or on its invalid parameter.

In the following example, the OnEntry advice should receive the name of the target method, but its methodName parameter has no binding attribute, so PostSharp expects a MethodExecutionArgs.

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

namespace Errors.LA0085_Error;

[PSerializable]
public class TraceMethodsAttribute : TypeLevelAspect
{
    // The parameter has no binding attribute, so PostSharp expects a MethodExecutionArgs.
    [OnMethodEntryAdvice, MulticastPointcut( Targets = MulticastTargets.Method )]
    public void OnEntry( string methodName )
    {
        Console.WriteLine( $"Entering {methodName}." );
    }
}

[TraceMethods]
public class OrderService
{
    public void PlaceOrder( string product ) => Console.WriteLine( $"Order placed: {product}" );
}

public static class Program
{
    public static void Main()
    {
        new OrderService().PlaceOrder( "Book" );
    }
}
error LA0085: LA0085_Error.cs(14,33): Advice "PostSharp.Aspects.Advices.OnMethodEntryAdvice" is not valid on method "Errors.LA0085_Error.TraceMethodsAttribute.OnEntry(System.String)": the method does not have the signature that this advice requires.

Resolution

In the following example, the DeclarationNameAttribute attribute binds the methodName parameter to the name of the target method.

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

namespace Errors.LA0085_Fixed;

[PSerializable]
public class TraceMethodsAttribute : TypeLevelAspect
{
    // The DeclarationName attribute binds the parameter to the name of the target method.
    [OnMethodEntryAdvice, MulticastPointcut( Targets = MulticastTargets.Method )]
    public void OnEntry( [DeclarationName] string methodName )
    {
        Console.WriteLine( $"Entering {methodName}." );
    }
}

[TraceMethods]
public class OrderService
{
    public void PlaceOrder( string product ) => Console.WriteLine( $"Order placed: {product}" );
}

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

See Also

Reference

OnMethodEntryAdvice
DeclarationNameAttribute
MethodExecutionArgs

Other Resources

Advice Methods
Advice Parameters