| Severity | Error |
| Reported by | PostSharp |
| Message | The advice "<Advice>" is in a group without pointcut, so it applies to the target of the aspect, "<Target>", but it cannot be applied to this kind of declaration. Add a pointcut to an advice of the group, for instance [MulticastPointcut]. |
Cause
A pointcut selects the declarations that a group of advices transforms. PostSharp puts an advice without pointcut in a group of its class: the group of an advice that has a pointcut, when there is one, or else a group without pointcut, which applies to the target of the aspect itself. A group without pointcut works only when its advices accept this kind of declaration. When they do not, PostSharp reports LA0049. The typical case is a method advice, such as an OnMethodEntryAdvice, in a TypeLevelAspect: the target of the aspect is a type, and the advice can only transform methods.
<Advice> is one advice of the group, and the message is reported on it: the advice that sets a property of the group, or else the first advice of the group. <Target> is the declaration to which the aspect is applied.
In the following example, the [Trace] aspect is applied to the OrderService class, and its entry advice has no pointcut.
using System;
using PostSharp.Aspects;
using PostSharp.Aspects.Advices;
using PostSharp.Serialization;
namespace Errors.LA0049_Error;
[PSerializable]
public class TraceAttribute : TypeLevelAspect
{
// The advice has no pointcut, so it applies to the class, but a method entry advice can only apply to methods.
[OnMethodEntryAdvice]
public void OnEntry( MethodExecutionArgs args )
{
Console.WriteLine( $"Entering {args.Method.Name}" );
}
}
[Trace]
public class OrderService
{
public void PlaceOrder( string product )
{
Console.WriteLine( $"Order placed for {product}" );
}
}
public static class Program
{
public static void Main()
{
new OrderService().PlaceOrder( "Coffee" );
}
}
error LA0049: LA0049_Error.cs(13,17): The advice "Errors.LA0049_Error.TraceAttribute.OnEntry(PostSharp.Aspects.MethodExecutionArgs)" is in a group without pointcut, so it applies to the target of the aspect, "Errors.LA0049_Error.OrderService", but it cannot be applied to this kind of declaration. Add a pointcut to an advice of the group, for instance [MulticastPointcut].
Resolution
- Add a pointcut that selects the declarations that the advices accept, for instance a MulticastPointcut that selects the methods of the target type, or a MethodPointcut that selects them with code. A group has one pointcut, which can be on any of its advices, and the other advices of the group apply to the same declarations.
- Or make the aspect a MethodLevelAspect and apply it to the methods, when the advices should transform only the target of the aspect.
In the following example, a MulticastPointcut selects the public methods of OrderService.
using System;
using PostSharp.Aspects;
using PostSharp.Aspects.Advices;
using PostSharp.Extensibility;
using PostSharp.Serialization;
namespace Errors.LA0049_Fixed;
[PSerializable]
public class TraceAttribute : TypeLevelAspect
{
// The pointcut selects the public methods of the class.
[OnMethodEntryAdvice, MulticastPointcut( Targets = MulticastTargets.Method, Attributes = MulticastAttributes.Public )]
public void OnEntry( MethodExecutionArgs args )
{
Console.WriteLine( $"Entering {args.Method.Name}" );
}
}
[Trace]
public class OrderService
{
public void PlaceOrder( string product )
{
Console.WriteLine( $"Order placed for {product}" );
}
}
public static class Program
{
public static void Main()
{
new OrderService().PlaceOrder( "Coffee" );
}
}
Entering PlaceOrder
Order placed for Coffee
See Also
Reference
MulticastPointcut
MethodPointcut
TypeLevelAspect
Other Resources