Open sandboxFocusImprove this doc

LA0066: Custom Attribute Derived From an Infrastructure Class of the Advices

Severity Error
Reported by PostSharp
Message Cannot find an advice analyzer for the custom attribute derived from RequiresAdviceAnalysisAttribute on "<Declaration>". This class is an infrastructure class of PostSharp: do not derive from it.

Cause

The RequiresAdviceAnalysisAttribute class of the PostSharp.Aspects.Internals namespace is an infrastructure class. PostSharp derives its own sealed custom attributes from it to mark the advice custom attributes whose methods it must analyze before weaving them, and registers an advice analyzer for each of them. When a method or a class carries a custom attribute derived from RequiresAdviceAnalysisAttribute for which no analyzer is registered, which is the case of any class derived from it in your code, PostSharp reports LA0066.

<Declaration> is the method or the class that carries the custom attribute. PostSharp reports the message without a location.

In the following example, the [Audited] custom attribute derives from RequiresAdviceAnalysisAttribute, which is a MulticastAttribute.

// @DisabledMessages(PS0131)

using System;
using System.Reflection;
using PostSharp.Aspects.Internals;
using PostSharp.Extensibility;

namespace Errors.LA0066_Error;

// A custom attribute derived from an infrastructure class of PostSharp.
[AttributeUsage( AttributeTargets.Method )]
[MulticastAttributeUsage( MulticastTargets.Method, PersistMetaData = true )]
public sealed class AuditedAttribute : RequiresAdviceAnalysisAttribute
{
}

public class OrderService
{
    // PostSharp looks for an advice analyzer for the attribute, and finds none.
    [Audited]
    public void PlaceOrder( string product )
    {
        Console.WriteLine( $"Order placed for {product}" );
    }
}

public static class Program
{
    public static void Main()
    {
        MethodInfo method = typeof(OrderService).GetMethod( nameof(OrderService.PlaceOrder) )!;
        Console.WriteLine( $"Audited: {method.IsDefined( typeof(AuditedAttribute) )}" );
    }
}
error LA0066: LA0066_Error.cs(21,17): Cannot find an advice analyzer for the custom attribute derived from RequiresAdviceAnalysisAttribute on "Errors.LA0066_Error.OrderService.PlaceOrder(System.String)". This class is an infrastructure class of PostSharp: do not derive from it.

Resolution

Do not derive from RequiresAdviceAnalysisAttribute. Derive your custom attribute from MulticastAttribute when it must be multicast, from Attribute otherwise, or from an aspect class when it must transform the code.

In the following example, the [Audited] custom attribute derives from MulticastAttribute, and the program reads it with reflection.

// @DisabledMessages(PS0131)

using System;
using System.Reflection;
using PostSharp.Extensibility;

namespace Errors.LA0066_Fixed;

// The custom attribute derives from MulticastAttribute.
[AttributeUsage( AttributeTargets.Method )]
[MulticastAttributeUsage( MulticastTargets.Method, PersistMetaData = true )]
public sealed class AuditedAttribute : MulticastAttribute
{
}

public class OrderService
{
    [Audited]
    public void PlaceOrder( string product )
    {
        Console.WriteLine( $"Order placed for {product}" );
    }
}

public static class Program
{
    public static void Main()
    {
        MethodInfo method = typeof(OrderService).GetMethod( nameof(OrderService.PlaceOrder) )!;
        Console.WriteLine( $"Audited: {method.IsDefined( typeof(AuditedAttribute) )}" );
    }
}
Audited: True

See Also

Reference

MulticastAttribute

Other Resources

Advice Methods