Open sandboxFocusImprove this doc

LA0253: Location-Level Aspect on an Extension Property

Severity Error
Reported by PostSharp
Message The aspect "<Aspect>" cannot be applied to the extension property "<Property>" because an extension property has no implementation of its own. Use a method-level aspect, which is applied to the accessors of the property.

Cause

The C# compiler implements a property of a C# 14 extension block as static accessor methods of the enclosing static class. The property that you declare in the extension block becomes a property of a compiler-generated metadata type, and this property has no implementation: its accessors only throw. An aspect that remains on this property, such as a LocationInterceptionAspect, therefore has nothing to intercept, and PostSharp reports LA0253 instead of applying it.

A method-level aspect, such as an OnMethodBoundaryAspect, does not remain on the property: PostSharp multicasts it to the implementations of the accessors, so it does not cause this message.

<Property> is the property of the metadata type, so its name contains the name of this compiler-generated type, which starts with <G>$.

PostSharp does not report LA0253, and skips the aspect without a message, when the UnsupportedTargetAction property of the aspect is Ignore.

In the following example, the [Trace] aspect, derived from LocationInterceptionAspect, is applied to the FullName extension property.

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

namespace Errors.LA0253_Error;

[PSerializable]
public class TraceAttribute : LocationInterceptionAspect
{
    public override void OnGetValue( LocationInterceptionArgs args )
    {
        Console.WriteLine( $"Getting {args.LocationName}." );
        args.ProceedGetValue();
    }
}

public class Customer
{
    public string FirstName { get; set; } = "Alice";

    public string LastName { get; set; } = "Smith";
}

public static class CustomerExtensions
{
    extension( Customer customer )
    {
        // A location-level aspect cannot be applied to an extension property.
        [Trace]
        public string FullName => $"{customer.FirstName} {customer.LastName}";
    }
}

public static class Program
{
    public static void Main()
    {
        Console.WriteLine( new Customer().FullName );
    }
}
error LA0253: LA0253_Error.cs(30,23): The aspect "Errors.LA0253_Error.TraceAttribute" cannot be applied to the extension property "Errors.LA0253_Error.CustomerExtensions.<G>$5ED366D4FBFDAC7D59B08CFFC12C4BE5.FullName" because an extension property has no implementation of its own. Use a method-level aspect, which is applied to the accessors of the property.

Resolution

Derive the aspect from a method-level aspect class, such as OnMethodBoundaryAspect or MethodInterceptionAspect, and apply it to the extension property or to its accessors. PostSharp applies it to the static methods that implement the accessors. In the advices, the method is the accessor, for example get_FullName, not the property.

In the following example, the [Trace] aspect derives from OnMethodBoundaryAspect, and its OnEntry advice runs when the program gets the extension property.

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

namespace Errors.LA0253_Fixed;

// A method-level aspect is applied to the accessors of the extension property.
[PSerializable]
public class TraceAttribute : OnMethodBoundaryAspect
{
    public override void OnEntry( MethodExecutionArgs args )
    {
        Console.WriteLine( $"Calling {args.Method.Name}." );
    }
}

public class Customer
{
    public string FirstName { get; set; } = "Alice";

    public string LastName { get; set; } = "Smith";
}

public static class CustomerExtensions
{
    extension( Customer customer )
    {
        [Trace]
        public string FullName => $"{customer.FirstName} {customer.LastName}";
    }
}

public static class Program
{
    public static void Main()
    {
        Console.WriteLine( new Customer().FullName );
    }
}
Calling get_FullName.
Alice Smith

See Also

Reference

LocationInterceptionAspect
OnMethodBoundaryAspect
UnsupportedTargetAction

Other Resources

C# 14 Extension Blocks and Multicasting
Intercepting Properties and Fields