Open sandboxFocusImprove this doc

PS0071: Attribute Applied to a Kind of Declaration It Does Not Support

Severity Error
Reported by PostSharp
Message Cannot apply the custom attribute "<Attribute>" to the <Kind> "<Declaration>": the attribute cannot be applied to this kind of declaration. Valid targets are: <ValidTargets>.

Cause

The ValidOn property of the MulticastAttributeUsageAttribute of an aspect class lists the kinds of declarations that the aspect can apply to, as MulticastTargets flags. It is often narrower than the AttributeUsageAttribute of C#, which the base aspect classes of PostSharp set broadly so that you can apply an aspect to a container, such as a class, and multicast it to its members.

When you apply the aspect directly to a declaration that is neither in ValidOn nor a container of such declarations, the C# compiler accepts the attribute, but PostSharp reports PS0071. For example, an aspect valid on properties only can be applied to a class, which multicasts it to the properties of the class, but not to a field.

<Kind> is the kind of the declaration, such as field, method or class, and <ValidTargets> lists the targets that the aspect allows.

In the following example, the [Trimmed] aspect is valid on properties only, and is applied to a field.

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

namespace Errors.PS0071_Error;

[PSerializable]
[MulticastAttributeUsage( MulticastTargets.Property )]
public class TrimmedAttribute : LocationInterceptionAspect
{
    public override void OnSetValue( LocationInterceptionArgs args )
    {
        args.Value = ((string?) args.Value)?.Trim();
        args.ProceedSetValue();
    }
}

public class Customer
{
    // The aspect can only be applied to properties, but this is a field.
    [Trimmed]
    public string? Name;
}

public static class Program
{
    public static void Main()
    {
        var customer = new Customer { Name = "  Alice  " };
        Console.WriteLine( $"[{customer.Name}]" );
    }
}
error PS0071: PS0071_Error.cs(23,20): Cannot apply the custom attribute "Errors.PS0071_Error.TrimmedAttribute" to the field "Errors.PS0071_Error.Customer/Name": the attribute cannot be applied to this kind of declaration. Valid targets are: Property.

Resolution

  • Apply the aspect to a declaration that <ValidTargets> lists, for example turn the field into a property, or apply the aspect to a container of such declarations.
  • When the aspect is designed to work on this kind of declaration, add it to the ValidOn property of the [MulticastAttributeUsage] of the aspect class. The base class of the aspect must allow it too, or PostSharp reports PS0072.

In the following example, Name is a property, and the aspect trims the value assigned to it.

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

namespace Errors.PS0071_Fixed;

[PSerializable]
[MulticastAttributeUsage( MulticastTargets.Property )]
public class TrimmedAttribute : LocationInterceptionAspect
{
    public override void OnSetValue( LocationInterceptionArgs args )
    {
        args.Value = ((string?) args.Value)?.Trim();
        args.ProceedSetValue();
    }
}

public class Customer
{
    // The aspect is applied to a property.
    [Trimmed]
    public string? Name { get; set; }
}

public static class Program
{
    public static void Main()
    {
        var customer = new Customer { Name = "  Alice  " };
        Console.WriteLine( $"[{customer.Name}]" );
    }
}
[Alice]

See Also

Reference

MulticastAttributeUsageAttribute
MulticastTargets

Other Resources

Adding Aspects to Multiple Declarations Using Attributes
Understanding Attribute Multicasting