Open sandboxFocusImprove this doc

LA0237: Get Advice Without a Location Value

Severity Error
Reported by PostSharp
Message The advice "<Advice>" cannot get the value of the location because it has neither a LocationInterceptionArgs parameter nor a [LocationValue] parameter.

Cause

An OnLocationGetValueAdvice replaces the getter of a field or property, so it must give the value that the getter returns. It gives it either through the LocationInterceptionArgs.Value property of a LocationInterceptionArgs parameter, which is the first parameter and has no binding attribute, or through a parameter annotated with LocationValueAttribute. When the advice has neither, PostSharp reports LA0237 on the advice method.

In the following example, the get advice of the [LogRead] aspect only binds the name of the property.

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

namespace Errors.LA0237_Error;

[PSerializable]
public class LogReadAttribute : LocationLevelAspect
{
    // The get advice has no parameter that gives the value of the property.
    [OnLocationGetValueAdvice]
    public static void OnGetValue( [DeclarationName] string propertyName )
    {
        Console.WriteLine( $"Reading {propertyName}." );
    }
}

public class Customer
{
    [LogRead]
    public string Name { get; set; } = "Jane Doe";
}

public static class Program
{
    public static void Main()
    {
        Console.WriteLine( new Customer().Name );
    }
}
error LA0237: LA0237_Error.cs(13,24): The advice "Errors.LA0237_Error.LogReadAttribute.OnGetValue(System.String)" cannot get the value of the location because it has neither a LocationInterceptionArgs parameter nor a [LocationValue] parameter.

Resolution

In the following example, the advice reads the property through its binding and assigns the [LocationValue] parameter.

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

namespace Errors.LA0237_Fixed;

[PSerializable]
public class LogReadAttribute : LocationLevelAspect
{
    // The [LocationValue] parameter receives the value that the binding reads from the property.
    [OnLocationGetValueAdvice]
    public static void OnGetValue( [DeclarationName] string propertyName,
                                   [This] object instance,
                                   [Binding] ILocationBinding<string> binding,
                                   [LocationValue] out string value )
    {
        Console.WriteLine( $"Reading {propertyName}." );
        value = binding.GetValue( instance );
    }
}

public class Customer
{
    [LogRead]
    public string Name { get; set; } = "Jane Doe";
}

public static class Program
{
    public static void Main()
    {
        Console.WriteLine( new Customer().Name );
    }
}
Reading Name.
Jane Doe

See Also

Reference

OnLocationGetValueAdvice
LocationValueAttribute
LocationInterceptionArgs

Other Resources

Advice Parameters