Open sandboxFocusImprove this doc

PS0172: Strict Inheritance Requested on a Property or Event

Severity Error
Reported by PostSharp
Message Cannot use inheritance of attribute '<AttributeType>' on <DeclarationKind> '<Declaration>': 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 PS0172 when you apply an attribute to a property or an event and set its AttributeInheritance property to Strict. <DeclarationKind> is property or event. PostSharp then stops processing the multicast attributes and also reports PS0055. When the MulticastAttributeUsageAttribute of the attribute class requests strict inheritance instead, PostSharp reports PS0171.

In the following example, the [Trim] aspect is applied to the virtual Name property with strict inheritance, so that it also applies to the overriding property.

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

namespace Errors.PS0172_Error;

[MulticastAttributeUsage( MulticastTargets.Property )]
[PSerializable]
public class TrimAttribute : LocationInterceptionAspect
{
    public override void OnSetValue( LocationInterceptionArgs args )
    {
        if ( args.Value is string text )
        {
            args.Value = text.Trim();
        }

        args.ProceedSetValue();
    }
}

public class Person
{
    // Strict inheritance is not supported on a property.
    [Trim( AttributeInheritance = MulticastInheritance.Strict )]
    public virtual string Name { get; set; } = "";
}

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

public static class Program
{
    public static void Main()
    {
        Customer customer = new() { Name = "  Alice  " };

        Console.WriteLine( $"[{customer.Name}]" );
    }
}
error PS0172: PS0172_Error.cs(27,27): Cannot use inheritance of attribute 'Errors.PS0172_Error.TrimAttribute' on property 'Errors.PS0172_Error.Person.Name': 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

  • Apply the aspect to each overriding or implementing property or event too.
  • Or apply the aspect to the base class with Multicast inheritance: PostSharp copies the aspect to each derived class, then multicasts it to the members of the derived class.

In the following example, the aspect is applied to the overriding property of Customer.

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

namespace Errors.PS0172_Fixed;

[MulticastAttributeUsage( MulticastTargets.Property )]
[PSerializable]
public class TrimAttribute : LocationInterceptionAspect
{
    public override void OnSetValue( LocationInterceptionArgs args )
    {
        if ( args.Value is string text )
        {
            args.Value = text.Trim();
        }

        args.ProceedSetValue();
    }
}

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

public class Customer : Person
{
    // The aspect is applied to the overriding property too.
    [Trim]
    public override string Name { get; set; } = "";
}

public static class Program
{
    public static void Main()
    {
        Customer customer = new() { Name = "  Alice  " };

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

See Also

Reference

AttributeInheritance
MulticastInheritance

Other Resources

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