Open sandboxFocusImprove this doc

PS0119: Multicast Pointcut Selects Unsupported Kinds of Declarations

Severity Error
Reported by PostSharp
Message Invalid MulticastPointcut attribute on the advice "<Advice>": this kind of advice cannot apply to the target(s) "<Targets>".

Cause

A MulticastPointcut selects the declarations to which an advice of a composite aspect applies. Its Targets property filters the kinds of declarations, but each kind of advice applies only to some of them, for example:

PostSharp reports PS0119, on the advice method <Advice>, when Targets contains other kinds. <Targets> lists the kinds that the advice does not support.

In the following example, an OnMethodEntryAdvice selects properties.

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

namespace Errors.PS0119_Error;

// Logs the entry into the methods of the target class.
[PSerializable]
public class LogMethodsAttribute : TypeLevelAspect
{
    // An OnMethodEntry advice applies to methods and constructors, not to properties.
    [OnMethodEntryAdvice, MulticastPointcut( Targets = MulticastTargets.Property )]
    public void OnEntry( MethodExecutionArgs args ) => Console.WriteLine( $"Entering {args.Method.Name}." );
}

[LogMethods]
public class OrderService
{
    public void PlaceOrder( string product ) => Console.WriteLine( $"Order for {product} placed." );
}

public static class Program
{
    public static void Main()
    {
        new OrderService().PlaceOrder( "Chair" );
    }
}
error PS0119: PS0119_Error.cs(15,17): Invalid MulticastPointcut attribute on the advice "Errors.PS0119_Error.LogMethodsAttribute.OnEntry(PostSharp.Aspects.MethodExecutionArgs)": this kind of advice cannot apply to the target(s) "Property".

Resolution

  • Set Targets to kinds that the advice supports, or do not set it: the advice then applies to every kind that it supports.
  • To enhance the accessors of properties, use the advices of locations (OnLocationGetValueAdvice, OnLocationSetValueAdvice) with a pointcut that selects properties.

In the following example, the pointcut selects methods.

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

namespace Errors.PS0119_Fixed;

// Logs the entry into the methods of the target class.
[PSerializable]
public class LogMethodsAttribute : TypeLevelAspect
{
    // The pointcut selects methods, which an OnMethodEntry advice supports.
    [OnMethodEntryAdvice, MulticastPointcut( Targets = MulticastTargets.Method )]
    public void OnEntry( MethodExecutionArgs args ) => Console.WriteLine( $"Entering {args.Method.Name}." );
}

[LogMethods]
public class OrderService
{
    public void PlaceOrder( string product ) => Console.WriteLine( $"Order for {product} placed." );
}

public static class Program
{
    public static void Main()
    {
        new OrderService().PlaceOrder( "Chair" );
    }
}
Entering PlaceOrder.
Order for Chair placed.

See Also

Reference

MulticastPointcut
MulticastTargets

Other Resources

Selecting Members with Pointcuts
Advice Methods