| Severity | Error |
| Reported by | PostSharp |
| Message | Invalid property <Property> for the multicast attribute "<AttributeType>" instantiated on <DeclarationKind> "<Declaration>": targets "<Targets>" are not supported by this custom attribute type. |
Cause
The MulticastAttributeUsageAttribute of an aspect or of another multicast attribute type defines the declarations that the attribute can be applied to: the kinds of declarations in ValidOn, and their visibility and scope in properties such as TargetMemberAttributes. When you apply the attribute, you can narrow these targets with the properties of MulticastAttribute, but you cannot extend them.
PostSharp reports PS0090 when one of the following properties of an instance of the attribute contains a value that the usage of the attribute type excludes. <Property> is the name of the property, and <Targets> lists the excluded values:
- AttributeTargetElements requests a kind of declaration that is not in ValidOn, for example properties for an OnMethodBoundaryAspect, which targets methods and constructors.
- AttributeTargetTypeAttributes, AttributeTargetMemberAttributes, AttributeTargetParameterAttributes, or their counterparts for external assemblies, request a visibility, a scope or another attribute that the usage of the attribute type excludes.
<DeclarationKind> and <Declaration> are the declaration that the attribute is applied to. PostSharp then stops the build with PS0055, because it does not continue after an error in the multicast attributes. When the attribute is applied to the assembly, the message has no location in the source code.
In the following example, the [Trace] aspect, an OnMethodBoundaryAspect, is multicast to the methods and the properties of Customer.
using System;
using PostSharp.Aspects;
using PostSharp.Extensibility;
using PostSharp.Serialization;
// A method-level aspect cannot target properties.
[assembly: Errors.PS0090_Error.Trace( AttributeTargetTypes = "Errors.PS0090_Error.Customer", AttributeTargetElements = MulticastTargets.Method | MulticastTargets.Property )]
namespace Errors.PS0090_Error;
[PSerializable]
public class TraceAttribute : OnMethodBoundaryAspect
{
public override void OnEntry( MethodExecutionArgs args )
{
Console.WriteLine( $"Entering {args.Method.Name}." );
}
}
public class Customer
{
public string Name { get; set; } = "Alice";
public void Greet() => Console.WriteLine( $"Hello, {this.Name}." );
}
public static class Program
{
public static void Main()
{
new Customer().Greet();
}
}
error PS0055: The processing of multicast attributes cannot continue because of the previous errors.
error PS0090: Invalid property AttributeTargetElements for the multicast attribute "Errors.PS0090_Error.TraceAttribute" instantiated on assembly "PS0090_Error": targets "Property" are not supported by this custom attribute type.
Resolution
- Remove the values that
<Targets>lists from the property. To apply a method-level aspect to properties, target methods: the accessors of the properties are methods. - When you wrote the attribute type, and it can support these targets, add them to its MulticastAttributeUsageAttribute.
In the following example, the aspect is multicast to the methods of Customer, which include the getter of the Name property.
using System;
using PostSharp.Aspects;
using PostSharp.Extensibility;
using PostSharp.Serialization;
// Targeting methods also targets the accessors of the properties.
[assembly: Errors.PS0090_Fixed.Trace( AttributeTargetTypes = "Errors.PS0090_Fixed.Customer", AttributeTargetElements = MulticastTargets.Method )]
namespace Errors.PS0090_Fixed;
[PSerializable]
public class TraceAttribute : OnMethodBoundaryAspect
{
public override void OnEntry( MethodExecutionArgs args )
{
Console.WriteLine( $"Entering {args.Method.Name}." );
}
}
public class Customer
{
public string Name { get; set; } = "Alice";
public void Greet() => Console.WriteLine( $"Hello, {this.Name}." );
}
public static class Program
{
public static void Main()
{
new Customer().Greet();
}
}
Entering Greet.
Entering get_Name.
Hello, Alice.
See Also
Reference
MulticastAttribute
MulticastAttributeUsageAttribute
MulticastTargets
MulticastAttributes
Other Resources
Adding Aspects to Multiple Declarations Using Attributes
Understanding Attribute Multicasting