Open sandboxFocusImprove this doc

LA0216: Negative Argument Index

Severity Error
Reported by PostSharp
Message The parameter "<Parameter>" of the advice "<Advice>" is bound by [Argument(<Index>)], but the index of an argument cannot be negative.

Cause

The ArgumentAttribute binding selects an argument of the target method by its index, its name or its type. An index is the zero-based position of the parameter in the target method, so it cannot be negative. In particular, -1 does not select the last argument. PostSharp reports LA0216 on the advice parameter.

In the following example, the advice selects the last argument with the index -1.

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

namespace Errors.LA0216_Error;

[PSerializable]
public class LogQuantityAttribute : MethodLevelAspect
{
    [OnMethodEntryAdvice]
    public static void OnEntry(
        [DeclarationName] string methodName,
        // A negative index does not select the last argument.
        [Argument( -1 )] int quantity )
    {
        Console.WriteLine( $"{methodName}: quantity {quantity}." );
    }
}

public class OrderService
{
    [LogQuantity]
    public void Order( string product, int quantity )
    {
        Console.WriteLine( $"Ordered {product}." );
    }
}

public static class Program
{
    public static void Main()
    {
        new OrderService().Order( "Coffee beans", 3 );
    }
}
error LA0216: LA0216_Error.cs(15,30): The parameter "quantity" of the advice "Errors.LA0216_Error.LogQuantityAttribute.OnEntry(System.String, System.Int32)" is bound by [Argument(-1)], but the index of an argument cannot be negative.

Resolution

Select the argument in another way:

  • By its name, with the ArgumentAttribute(string) constructor. The name works on target methods whose parameters are in a different order.
  • By its zero-based index, with the ArgumentAttribute(int) constructor.
  • By its type, with the parameterless constructor, when the target method has a single parameter of the type of the advice parameter.

In the following example, the advice selects the argument by its name.

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

namespace Errors.LA0216_Fixed;

[PSerializable]
public class LogQuantityAttribute : MethodLevelAspect
{
    [OnMethodEntryAdvice]
    public static void OnEntry(
        [DeclarationName] string methodName,
        // The argument is selected by its name.
        [Argument( "quantity" )] int quantity )
    {
        Console.WriteLine( $"{methodName}: quantity {quantity}." );
    }
}

public class OrderService
{
    [LogQuantity]
    public void Order( string product, int quantity )
    {
        Console.WriteLine( $"Ordered {product}." );
    }
}

public static class Program
{
    public static void Main()
    {
        new OrderService().Order( "Coffee beans", 3 );
    }
}
Order: quantity 3.
Ordered Coffee beans.

See Also

Reference

ArgumentAttribute

Other Resources

Advice Parameters