Open sandboxFocusImprove this doc

LA0207: Advice Parameter Has Several Binding Attributes

Severity Error
Reported by PostSharp
Message The parameter "<Parameter>" of the advice "<Advice>" has more than one advice parameter attribute.

Cause

The parameters of an advice method receive their values from attributes derived from AdviceParameterAttribute: [ArgumentAttribute] receives an argument of the target method, [DeclarationNameAttribute] its name, [ReturnValueAttribute] its return value, and so on. Each attribute selects one value, so a parameter can have only one of them. PostSharp reports LA0207 when a parameter <Parameter> of the advice method <Advice> has two or more.

In the following example, the methodName parameter of the OnEntry advice has both [DeclarationNameAttribute] and [ArgumentAttribute].

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

namespace Errors.LA0207_Error;

[PSerializable]
public class LogCustomerAttribute : MethodLevelAspect
{
    [OnMethodEntryAdvice]
    public static void OnEntry(
        // One parameter cannot receive both the name of the method and its first argument.
        [DeclarationName] [Argument( 0 )] string methodName )
    {
        Console.WriteLine( $"Entering {methodName}." );
    }
}

public class CustomerService
{
    [LogCustomer]
    public void Register( string customerId )
    {
        Console.WriteLine( $"Customer {customerId} registered." );
    }
}

public static class Program
{
    public static void Main()
    {
        new CustomerService().Register( "C-042" );
    }
}
error LA0207: LA0207_Error.cs(14,50): The parameter "methodName" of the advice "Errors.LA0207_Error.LogCustomerAttribute.OnEntry(System.String)" has more than one advice parameter attribute.

Resolution

Give each value a parameter of its own, with one advice parameter attribute.

In the following example, the advice receives the name of the method and the first argument in two parameters.

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

namespace Errors.LA0207_Fixed;

[PSerializable]
public class LogCustomerAttribute : MethodLevelAspect
{
    [OnMethodEntryAdvice]
    public static void OnEntry(
        // Each value is bound to a parameter of its own.
        [DeclarationName] string methodName, [Argument( 0 )] string customerId )
    {
        Console.WriteLine( $"Entering {methodName} for customer {customerId}." );
    }
}

public class CustomerService
{
    [LogCustomer]
    public void Register( string customerId )
    {
        Console.WriteLine( $"Customer {customerId} registered." );
    }
}

public static class Program
{
    public static void Main()
    {
        new CustomerService().Register( "C-042" );
    }
}
Entering Register for customer C-042.
Customer C-042 registered.

See Also

Reference

AdviceParameterAttribute
OnMethodEntryAdvice

Other Resources

Advice Parameters