Open sandboxFocusImprove this doc

LA0213: Bound Advice Parameter Passed by Reference

Severity Error
Reported by PostSharp
Message The parameter "<Parameter>" of the advice "<Advice>" cannot be passed by reference because it is bound by <Binding>.

Cause

Each binding attribute of an advice parameter accepts some parameter modes, which can depend on the advice. Many bindings give a value that the advice reads but cannot replace, so they accept only parameters passed by value: for example ArgumentsAttribute, BindingAttribute, DeclarationAttribute and DeclarationNameAttribute. PostSharp reports LA0213 when a parameter with such a binding is declared as in, ref or out. <Binding> is the binding attribute, such as [Arguments].

When the binding requires a parameter passed by reference, or accepts only out and ref, and the parameter is passed by value or as in, PostSharp reports LA0212 instead.

In the following example, the interception advice receives the Arguments object as a ref parameter, to change the arguments before it calls the intercepted method. Because the parameter is passed by reference, PostSharp reports the message without a file and line, but the message names the parameter and the advice.

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

namespace Errors.LA0213_Error;

[PSerializable]
public class TrimArgumentsAttribute : MethodLevelAspect
{
    [OnMethodInvokeAdvice]
    public static void OnInvoke(
        [This] object instance,
        [Binding] IMethodBinding binding,
        // The Arguments object cannot be passed by reference.
        [Arguments] ref Arguments arguments,
        [ReturnValue] out string returnValue )
    {
        for ( int i = 0; i < arguments.Count; i++ )
        {
            if ( arguments[i] is string text )
            {
                arguments[i] = text.Trim();
            }
        }

        returnValue = (string) binding.Invoke( ref instance, arguments );
    }
}

public class GreetingService
{
    [TrimArguments]
    public string Greet( string name ) => $"Hello, {name}.";
}

public static class Program
{
    public static void Main()
    {
        Console.WriteLine( new GreetingService().Greet( "  Alice  " ) );
    }
}
error LA0213: LA0213_Error.cs(16,35): The parameter "arguments" of the advice "Errors.LA0213_Error.TrimArgumentsAttribute.OnInvoke(System.Object, PostSharp.Aspects.IMethodBinding, PostSharp.Aspects.Arguments@, System.String@)" cannot be passed by reference because it is bound by [Arguments].

Resolution

Remove the in, ref or out modifier from the parameter. To change a value that the binding gives by value, change the object itself: Arguments is a class, so the advice can set its items, and the intercepted method receives the changed arguments when the advice passes the object to the binding. The table of Binding attributes lists the modes that each attribute accepts.

In the following example, the advice receives the Arguments object by value and trims its strings.

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

namespace Errors.LA0213_Fixed;

[PSerializable]
public class TrimArgumentsAttribute : MethodLevelAspect
{
    [OnMethodInvokeAdvice]
    public static void OnInvoke(
        [This] object instance,
        [Binding] IMethodBinding binding,
        // The Arguments object is passed by value, and the advice changes its content.
        [Arguments] Arguments arguments,
        [ReturnValue] out string returnValue )
    {
        for ( int i = 0; i < arguments.Count; i++ )
        {
            if ( arguments[i] is string text )
            {
                arguments[i] = text.Trim();
            }
        }

        returnValue = (string) binding.Invoke( ref instance, arguments );
    }
}

public class GreetingService
{
    [TrimArguments]
    public string Greet( string name ) => $"Hello, {name}.";
}

public static class Program
{
    public static void Main()
    {
        Console.WriteLine( new GreetingService().Greet( "  Alice  " ) );
    }
}
Hello, Alice.

See Also

Reference

ArgumentsAttribute
AdviceParameterAttribute

Other Resources

Advice Parameters