Open sandboxFocusImprove this doc

PS0171: Strict Inheritance in the Usage of an Attribute That Targets Properties or Events

Severity Error
Reported by PostSharp
Message Error in MulticastAttributeUsage applied on type '<AttributeType>': strict attribute inheritance is not supported on properties and events.

Cause

Aspect inheritance makes PostSharp apply an aspect to the declarations that derive from the one where you apply it, as Adding Aspects to Derived Classes and Methods Using Attributes describes. With Strict inheritance, an aspect on a method also applies to the methods that override or implement it. PostSharp does not support this on properties and events.

PostSharp reports PS0171 when the MulticastAttributeUsageAttribute of an attribute class sets Inheritance to Strict, and its ValidOn includes Property or Event, alone or with other targets. The message is reported on the attribute class <AttributeType>, whether or not the attribute is applied to a property or an event. PostSharp then stops processing the multicast attributes and also reports PS0055.

In the following example, the [Trim] aspect targets properties and requests strict inheritance.

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

namespace Errors.PS0171_Error;

// Strict inheritance is not supported on properties.
[MulticastAttributeUsage( MulticastTargets.Property, Inheritance = MulticastInheritance.Strict )]
[PSerializable]
public class TrimAttribute : LocationInterceptionAspect
{
    public override void OnSetValue( LocationInterceptionArgs args )
    {
        if ( args.Value is string text )
        {
            args.Value = text.Trim();
        }

        args.ProceedSetValue();
    }
}

[Trim]
public class Person
{
    public string Name { get; set; } = "";
}

public class Customer : Person
{
    public string Email { get; set; } = "";
}

public static class Program
{
    public static void Main()
    {
        Customer customer = new() { Name = "  Alice  ", Email = " [email protected] " };

        Console.WriteLine( $"[{customer.Name}] [{customer.Email}]" );
    }
}
error PS0171: PS0171_Error.cs(11,14): Error in MulticastAttributeUsage applied on type 'Errors.PS0171_Error.TrimAttribute': strict attribute inheritance is not supported on properties and events.
error PS0055: The processing of multicast attributes cannot continue because of the previous errors.

Resolution

  • To apply the aspect to the properties or events of derived classes, use Multicast inheritance and apply the aspect to the base class: PostSharp copies the aspect to each derived class, then multicasts it to the members of the derived class, including the members that the base class does not have.
  • Otherwise, remove Inheritance from the usage, and apply the aspect to each property or event, including the overriding ones.
  • When the aspect needs strict inheritance for methods, remove the properties and events from ValidOn.

In the following example, the aspect uses multicast inheritance, so it applies to the properties of Person and of Customer.

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

namespace Errors.PS0171_Fixed;

// Multicast inheritance copies the aspect from Person to Customer, then to the properties of Customer.
[MulticastAttributeUsage( MulticastTargets.Property, Inheritance = MulticastInheritance.Multicast )]
[PSerializable]
public class TrimAttribute : LocationInterceptionAspect
{
    public override void OnSetValue( LocationInterceptionArgs args )
    {
        if ( args.Value is string text )
        {
            args.Value = text.Trim();
        }

        args.ProceedSetValue();
    }
}

[Trim]
public class Person
{
    public string Name { get; set; } = "";
}

public class Customer : Person
{
    public string Email { get; set; } = "";
}

public static class Program
{
    public static void Main()
    {
        Customer customer = new() { Name = "  Alice  ", Email = " [email protected] " };

        Console.WriteLine( $"[{customer.Name}] [{customer.Email}]" );
    }
}

See Also

Reference

MulticastAttributeUsageAttribute
MulticastInheritance

Other Resources

Adding Aspects to Derived Classes and Methods Using Attributes
Understanding Aspect Inheritance