| Severity | Warning |
| Reported by | PostSharp |
| Message | The <Property> property of the advice "<Advice>" is redundant: without it, the advice is in the same group. Remove the <Property> property. |
Cause
PostSharp groups the advices of each class of an aspect separately. An advice without pointcut and without the Group property joins the group of its class that has no advice of its kind yet, or starts a group that applies to the target of the aspect. The Group property selects a group by name, which is needed only when this inference gives another group or cannot choose one.
PostSharp reports LA0255 when the groups of the class would be the same without the Group property of the advice. <Property> is always Group. When several advices are reported, each property is redundant together with the ones reported before it, so you can remove all of them.
A class of the aspect can join a group of another class by its name. PostSharp does not report a Group property when a base class of the aspect, or a class of the same project derived from the class of the advice, selects the same group name.
In the following example, the TraceAttribute aspect puts its two advices in the Trace group, which is the group that PostSharp infers anyway.
using System;
using PostSharp.Aspects;
using PostSharp.Aspects.Advices;
using PostSharp.Serialization;
namespace Errors.LA0255_Error;
[PSerializable]
public class TraceAttribute : MethodLevelAspect
{
// Both advices would be in one group without the Group property.
[OnMethodEntryAdvice( Group = "Trace" )]
public void OnEntry( MethodExecutionArgs args ) => Console.WriteLine( $"Entering {args.Method.Name}" );
[OnMethodExitAdvice( Group = "Trace" )]
public void OnExit( MethodExecutionArgs args ) => Console.WriteLine( $"Leaving {args.Method.Name}" );
}
public class OrderService
{
[Trace]
public void PlaceOrder() => Console.WriteLine( "Placing the order" );
}
public static class Program
{
public static void Main()
{
new OrderService().PlaceOrder();
}
}
warning LA0255: LA0255_Error.cs(13,17): The Group property of the advice "Errors.LA0255_Error.TraceAttribute.OnEntry(PostSharp.Aspects.MethodExecutionArgs)" is redundant: without it, the advice is in the same group. Remove the Group property.
warning LA0255: LA0255_Error.cs(16,17): The Group property of the advice "Errors.LA0255_Error.TraceAttribute.OnExit(PostSharp.Aspects.MethodExecutionArgs)" is redundant: without it, the advice is in the same group. Remove the Group property.
Resolution
Remove the Group property of the reported advices. If a derived class in another project joins the group by its name, keep the property, and disable the warning for this advice.
In the following example, the exit advice joins the group of the entry advice without the property.
using System;
using PostSharp.Aspects;
using PostSharp.Aspects.Advices;
using PostSharp.Serialization;
namespace Errors.LA0255_Fixed;
[PSerializable]
public class TraceAttribute : MethodLevelAspect
{
// Without the Group property, the exit advice joins the group of the entry advice.
[OnMethodEntryAdvice]
public void OnEntry( MethodExecutionArgs args ) => Console.WriteLine( $"Entering {args.Method.Name}" );
[OnMethodExitAdvice]
public void OnExit( MethodExecutionArgs args ) => Console.WriteLine( $"Leaving {args.Method.Name}" );
}
public class OrderService
{
[Trace]
public void PlaceOrder() => Console.WriteLine( "Placing the order" );
}
public static class Program
{
public static void Main()
{
new OrderService().PlaceOrder();
}
}
Entering PlaceOrder
Placing the order
Leaving PlaceOrder
See Also
Reference
Group
OnMethodEntryAdvice
OnMethodExitAdvice
Other Resources