Open sandboxFocusImprove this doc

LA0204: Aspect Applied to a Function Pointer

Severity Error
Reported by PostSharp
Message Cannot apply the aspect "<Aspect>" to the target element "<Target>". Applying aspects to fields, properties, and parameters of method pointer type is not supported.

Cause

PostSharp does not support function pointers, declared with delegate* in unsafe C# code, as the type of an element that an aspect intercepts or whose value it receives. It reports LA0204 when an aspect or an advice targets:

<Target> is the element that the aspect or the advice targets.

In the following example, the [TraceAccess] aspect is applied to a field of type delegate*<decimal, decimal>.

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

namespace Errors.LA0204_Error;

[PSerializable]
public class TraceAccessAttribute : LocationInterceptionAspect
{
    public override void OnGetValue( LocationInterceptionArgs args )
    {
        Console.WriteLine( $"Reading {args.LocationName}." );
        base.OnGetValue( args );
    }
}

public static unsafe class PriceCalculator
{
    // The field has a function pointer type, which the aspect cannot intercept.
    [TraceAccess]
    public static delegate*<decimal, decimal> Discount = &HalfPrice;

    private static decimal HalfPrice( decimal price ) => price / 2;

    public static decimal GetPrice( decimal price ) => Discount( price );
}

public static class Program
{
    public static void Main()
    {
        Console.WriteLine( PriceCalculator.GetPrice( 100 ) );
    }
}
error LA0204: LA0204_Error.cs(21,47): Cannot apply the aspect "Errors.LA0204_Error.TraceAccessAttribute" to the target element "Errors.LA0204_Error.PriceCalculator.Discount". Applying aspects to fields, properties, and parameters of method pointer type is not supported.

Resolution

  • Change the type of the field, property or parameter to a delegate type, such as Func<T, TResult>, which PostSharp supports.
  • Otherwise, exclude the element from the aspect. When the aspect is multicast, exclude the element with AttributeExclude, or set the UnsupportedTargetAction property of the aspect (or the UnsupportedTargetAction property of the advice, in a composite aspect) to Ignore, so that PostSharp skips the unsupported elements without a message.

In the following example, the field has the type Func<decimal, decimal>.

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

namespace Errors.LA0204_Fixed;

[PSerializable]
public class TraceAccessAttribute : LocationInterceptionAspect
{
    public override void OnGetValue( LocationInterceptionArgs args )
    {
        Console.WriteLine( $"Reading {args.LocationName}." );
        base.OnGetValue( args );
    }
}

public static class PriceCalculator
{
    // The field has a delegate type, which the aspect can intercept.
    [TraceAccess]
    public static Func<decimal, decimal> Discount = HalfPrice;

    private static decimal HalfPrice( decimal price ) => price / 2;

    public static decimal GetPrice( decimal price ) => Discount( price );
}

public static class Program
{
    public static void Main()
    {
        Console.WriteLine( PriceCalculator.GetPrice( 100 ) );
    }
}
Reading Discount.
50

See Also

Reference

LocationInterceptionAspect
OnMethodBoundaryAspect

Other Resources

Intercepting Properties and Fields