Open sandboxFocusImprove this doc

LA0258: Advice Sets Both Group and Master

Severity Error
Reported by PostSharp
Message The advice "<Advice>" sets both the Group and the Master property. Set only one of them.

Cause

An advice selects its group explicitly with the Group property, which names the group. The Master property, which puts the advice in the group of another advice, is the former way of doing it. It is obsolete, so the C# compiler reports CS0618 when you use it, but it keeps working.

The two properties are two ways of selecting the group of an advice, so PostSharp reports LA0258 when an advice sets both.

In the following example, the exit advice sets both properties. The sample disables CS0618 to show only LA0258.

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

#pragma warning disable CS0618 // Master is obsolete.

namespace Errors.LA0258_Error;

[PSerializable]
public class TraceAttribute : MethodLevelAspect
{
    [OnMethodEntryAdvice( Group = "Trace" )]
    public void OnEntry( MethodExecutionArgs args ) => Console.WriteLine( $"Entering {args.Method.Name}" );

    // The advice selects its group with both Group and the obsolete Master property.
    [OnMethodExitAdvice( Group = "Trace", Master = nameof( OnEntry ) )]
    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();
    }
}
error LA0258: LA0258_Error.cs(18,17): The advice "Errors.LA0258_Error.TraceAttribute.OnExit(PostSharp.Aspects.MethodExecutionArgs)" sets both the Group and the Master property. Set only one of them.

Resolution

Remove the Master property. When the class has only one group that has no advice of the kind of the advice, PostSharp puts the advice in this group, and the Group property is not needed either: PostSharp reports it with the warning LA0255. Otherwise, keep the Group property, and set the same name on all the advices of the group.

In the following example, the aspect has one group, and the two advices are in it without Group or Master.

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

namespace Errors.LA0258_Fixed;

[PSerializable]
public class TraceAttribute : MethodLevelAspect
{
    [OnMethodEntryAdvice]
    public void OnEntry( MethodExecutionArgs args ) => Console.WriteLine( $"Entering {args.Method.Name}" );

    // Without Group and Master, the advice joins the only group of the class that has no exit advice.
    [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
Master

Other Resources

Selecting Members with Pointcuts
Advice Methods