| Severity | Error |
| Reported by | PostSharp |
| Message | The parameter "<Parameter>" of the advice "<Advice>" has an advice parameter attribute, but advices of the kind "<AdviceKind>" do not accept advice parameter bindings. |
Cause
Only some kinds of advices accept parameters annotated with a binding attribute: the method boundary advices, OnMethodInvokeAdvice, OnLocationGetValueAdvice, OnLocationSetValueAdvice and OnInstanceConstructedAdvice. The code that PostSharp generates for the other advices passes them only their AdviceArgs object. When a parameter of such an advice has a binding attribute, PostSharp cannot pass a value to it, and reports LA0209 on the parameter.
The advices that do not accept bound parameters include OnInstanceLocationInitializedAdvice, OnMethodInvokeAsyncAdvice and the event interception advices. <AdviceKind> is the full name of the attribute of the advice.
In the following example, the initialization advice of the [LogInitialValue] aspect has a [DeclarationName] parameter.
using System;
using PostSharp.Aspects;
using PostSharp.Aspects.Advices;
using PostSharp.Serialization;
namespace Errors.LA0209_Error;
[PSerializable]
public class LogInitialValueAttribute : LocationLevelAspect
{
[OnInstanceLocationInitializedAdvice]
// An initialization advice receives only LocationInitializationArgs: it does not accept [DeclarationName].
public void OnInitialized( LocationInitializationArgs args, [DeclarationName] string fieldName )
{
Console.WriteLine( $"{fieldName} is initialized to {args.Value}." );
}
}
public class Customer
{
[LogInitialValue]
public string Name = "Alice";
}
public static class Program
{
public static void Main()
{
Console.WriteLine( new Customer().Name );
}
}
error LA0209: LA0209_Error.cs(13,90): The parameter "fieldName" of the advice "Errors.LA0209_Error.LogInitialValueAttribute.OnInitialized(PostSharp.Aspects.LocationInitializationArgs, System.String)" has an advice parameter attribute, but advices of the kind "PostSharp.Aspects.Advices.OnInstanceLocationInitializedAdvice" do not accept advice parameter bindings.
Resolution
- Remove the bound parameter, and read the value from the AdviceArgs object of the advice. For instance, LocationInitializationArgs gives the name of the field or property and its value.
- Otherwise, move the parameter to an advice of a kind that accepts it. The table in Bindings by advice kind shows the binding attributes that each advice accepts.
In the following example, the advice reads the name of the field from LocationInitializationArgs.
using System;
using PostSharp.Aspects;
using PostSharp.Aspects.Advices;
using PostSharp.Serialization;
namespace Errors.LA0209_Fixed;
[PSerializable]
public class LogInitialValueAttribute : LocationLevelAspect
{
[OnInstanceLocationInitializedAdvice]
// The name of the field comes from LocationInitializationArgs instead of a bound parameter.
public void OnInitialized( LocationInitializationArgs args )
{
Console.WriteLine( $"{args.LocationName} is initialized to {args.Value}." );
}
}
public class Customer
{
[LogInitialValue]
public string Name = "Alice";
}
public static class Program
{
public static void Main()
{
Console.WriteLine( new Customer().Name );
}
}
Name is initialized to Alice.
Alice
See Also
Reference
AdviceParameterAttribute
OnInstanceLocationInitializedAdvice
LocationInitializationArgs
Other Resources