Open sandboxFocusImprove this doc

LA0208: Advice Parameter Without Binding Attribute

Severity Error
Reported by PostSharp
Message The parameter "<Parameter>" of the advice "<Advice>" has no advice parameter attribute. Only the first parameter of a method boundary or interception advice can be passed without attribute, and it must then be of the AdviceArgs type of the advice.

Cause

An advice method receives each value that it needs through a parameter annotated with a binding attribute, such as ArgumentAttribute or DeclarationNameAttribute. The attribute tells PostSharp which value to pass. The only parameter that can have no binding attribute is the first parameter of a method boundary, method interception or location interception advice, which then receives the AdviceArgs object of the advice, for instance MethodExecutionArgs.

PostSharp reports LA0208 on a parameter without binding attribute in the following cases:

  • The parameter is not the first parameter of a method boundary or interception advice, for instance when you add a parameter for an argument of the target method and forget the [Argument] attribute.
  • The parameter belongs to an OnInstanceConstructedAdvice, which receives no AdviceArgs object, so even its first parameter needs a binding attribute.

The rules of an advice signature are described in Rules of an advice signature.

In the following example, the orderId parameter of the entry advice has no binding attribute.

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

namespace Errors.LA0208_Error;

[PSerializable]
public class LogAttribute : MethodLevelAspect
{
    [OnMethodEntryAdvice]
    // The orderId parameter has no binding attribute, so PostSharp does not know which value to pass.
    public void OnEntry( [DeclarationName] string methodName, int orderId )
    {
        Console.WriteLine( $"{methodName}( {orderId} )" );
    }
}

public class OrderService
{
    [Log]
    public void Ship( int orderId )
    {
        Console.WriteLine( $"Shipping order {orderId}." );
    }
}

public static class Program
{
    public static void Main()
    {
        new OrderService().Ship( 42 );
    }
}
error LA0208: LA0208_Error.cs(13,67): The parameter "orderId" of the advice "Errors.LA0208_Error.LogAttribute.OnEntry(System.String, System.Int32)" has no advice parameter attribute. Only the first parameter of a method boundary or interception advice can be passed without attribute, and it must then be of the AdviceArgs type of the advice.

Resolution

  • Add the binding attribute that selects the value of the parameter, for instance [Argument( 0 )] for the first argument of the target method. The table in Binding attributes lists the values that you can bind.
  • Otherwise, remove the parameter.

In the following example, [Argument( 0 )] binds the orderId parameter to the first argument of the target method.

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

namespace Errors.LA0208_Fixed;

[PSerializable]
public class LogAttribute : MethodLevelAspect
{
    [OnMethodEntryAdvice]
    // [Argument( 0 )] binds the orderId parameter to the first argument of the target method.
    public void OnEntry( [DeclarationName] string methodName, [Argument( 0 )] int orderId )
    {
        Console.WriteLine( $"{methodName}( {orderId} )" );
    }
}

public class OrderService
{
    [Log]
    public void Ship( int orderId )
    {
        Console.WriteLine( $"Shipping order {orderId}." );
    }
}

public static class Program
{
    public static void Main()
    {
        new OrderService().Ship( 42 );
    }
}
Ship( 42 )
Shipping order 42.

See Also

Reference

AdviceParameterAttribute
ArgumentAttribute
AdviceArgs

Other Resources

Advice Parameters
Advice Methods