| 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:
- In a method boundary, method interception or location interception advice, such as OnMethodEntryAdvice, OnMethodInvokeAdvice or OnLocationGetValueAdvice, the first parameter has no binding attribute and is not of the legacy type of the advice: MethodExecutionArgs, MethodInterceptionArgs or LocationInterceptionArgs. A parameter without binding attribute after the first one causes LA0208 instead.
- In an event interception advice, such as OnEventAddHandlerAdvice, the method does not have exactly one parameter of type EventInterceptionArgs. These advices do not accept bound parameters.
- In a LocationValidationAdvice, the method does not have the signature of ValidateValue(T, string, LocationKind, LocationValidationContext): four parameters, of which the last three are a string, a LocationKind and a LocationValidationContext, and the return type Exception.
<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
- Add a binding attribute, such as DeclarationNameAttribute or ArgumentAttribute, to each parameter that receives a value of the target.
- In an advice that does not accept bound parameters, give the method the parameter of the legacy signature, for example EventInterceptionArgs for an event interception advice.
- In a LocationValidationAdvice, give the method the parameters and the return type of ValidateValue(T, string, LocationKind, LocationValidationContext).
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