Open sandboxFocusImprove this doc

PS0091: Attribute Applied to an Excluded Declaration

Severity Error
Reported by PostSharp
Message Cannot apply the custom attribute "<AttributeType>" to <DeclarationKind> "<Declaration>": the attribute cannot be applied to this kind of declaration (<Reason>).

Cause

The MulticastAttributeUsageAttribute of an aspect or of another multicast attribute type can restrict the declarations that the attribute applies to by their visibility, their scope or other attributes, with properties such as TargetMemberAttributes and TargetTypeAttributes. When the attribute is multicast, for example from a class to its methods, PostSharp silently skips the declarations that do not match. When you apply the attribute directly to a declaration that does not match, PostSharp reports PS0091 on this declaration.

<Reason> describes the attribute of the declaration that the usage excludes, for example static member, instance member, private member, abstract member or static type. Some aspects of PostSharp have such a usage: for example, an InstanceLevelAspect cannot be applied to a static class.

In the following example, the [Audit] aspect accepts only instance methods, and it is applied to the static Create method.

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

namespace Errors.PS0091_Error;

// The aspect accepts only instance methods.
[PSerializable]
[MulticastAttributeUsage( MulticastTargets.Method, TargetMemberAttributes = MulticastAttributes.Instance )]
public class AuditAttribute : OnMethodBoundaryAspect
{
    public override void OnEntry( MethodExecutionArgs args )
    {
        Console.WriteLine( $"Audit: {args.Method.Name} called on {args.Instance}." );
    }
}

public class OrderService
{
    public override string ToString() => "the order service";

    // The aspect is applied to a static method.
    [Audit]
    public static OrderService Create() => new();

    public void PlaceOrder() => Console.WriteLine( "Order placed." );
}

public static class Program
{
    public static void Main()
    {
        OrderService.Create().PlaceOrder();
    }
}
error PS0091: PS0091_Error.cs(25,32): Cannot apply the custom attribute "Errors.PS0091_Error.AuditAttribute" to method "Errors.PS0091_Error.OrderService.Create": the attribute cannot be applied to this kind of declaration (static member).

Resolution

  • Apply the attribute to a declaration that its usage accepts, or apply it to the containing type and let multicasting skip the declarations that do not match.
  • When you wrote the attribute type, and it can support this kind of declaration, extend its MulticastAttributeUsageAttribute.

In the following example, the aspect is applied to the instance method PlaceOrder.

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

namespace Errors.PS0091_Fixed;

// The aspect accepts only instance methods.
[PSerializable]
[MulticastAttributeUsage( MulticastTargets.Method, TargetMemberAttributes = MulticastAttributes.Instance )]
public class AuditAttribute : OnMethodBoundaryAspect
{
    public override void OnEntry( MethodExecutionArgs args )
    {
        Console.WriteLine( $"Audit: {args.Method.Name} called on {args.Instance}." );
    }
}

public class OrderService
{
    public override string ToString() => "the order service";

    public static OrderService Create() => new();

    // The aspect is applied to an instance method.
    [Audit]
    public void PlaceOrder() => Console.WriteLine( "Order placed." );
}

public static class Program
{
    public static void Main()
    {
        OrderService.Create().PlaceOrder();
    }
}
Audit: PlaceOrder called on the order service.
Order placed.

See Also

Reference

MulticastAttributeUsageAttribute
MulticastAttributes

Other Resources

Adding Aspects to Multiple Declarations Using Attributes
Understanding Attribute Multicasting