| Severity | Error |
| Reported by | PostSharp |
| Message | An aspect class and its base class cannot both define an advice method named "<Name>": it is defined in "<BaseType>" and in "<DerivedType>". Rename one of the advice methods. |
Cause
PostSharp collects the advices of an aspect from the aspect class and from all its base classes, and it identifies advice methods by their name, for example in AdviceDependencyAttribute, which orders an advice with respect to another one designated by the name of its method. Therefore, the advice methods declared in different classes of the hierarchy must have different names. Overloads declared in the same class are not affected.
PostSharp reads the advices of the aspect class first, then those of its base classes, so it reports LA0132 on the advice method of the base class, <BaseType>, and ignores this advice. <DerivedType> is the class that declares the first advice method of this name. The message is typical of an advice method that hides the advice method of its base class with the new modifier.
In the following example, the TraceAttribute aspect class and its base class both have an advice method named Trace.
using System;
using PostSharp.Aspects;
using PostSharp.Aspects.Advices;
using PostSharp.Serialization;
namespace Errors.LA0132_Error;
[PSerializable]
public abstract class TraceAspectBase : MethodLevelAspect
{
// The derived aspect class defines another advice method named Trace.
[OnMethodEntryAdvice]
public void Trace( [DeclarationName] string methodName )
{
Console.WriteLine( $"Entering {methodName}." );
}
}
[PSerializable]
public class TraceAttribute : TraceAspectBase
{
[OnMethodExitAdvice]
public new void Trace( [DeclarationName] string methodName )
{
Console.WriteLine( $"Leaving {methodName}." );
}
}
public class OrderService
{
[Trace]
public void PlaceOrder()
{
Console.WriteLine( "Placing the order." );
}
}
public static class Program
{
public static void Main()
{
new OrderService().PlaceOrder();
}
}
error LA0132: LA0132_Error.cs(13,17): An aspect class and its base class cannot both define an advice method named "Trace": it is defined in "Errors.LA0132_Error.TraceAspectBase" and in "Errors.LA0132_Error.TraceAttribute". Rename one of the advice methods.
Resolution
Rename one of the advice methods, so that each advice method of the aspect hierarchy has a name of its own. To change the behavior of an advice in a derived aspect class, make the advice call a virtual method and override that method instead.
In the following example, the advice method of the derived class is renamed TraceExit, so the aspect applies both advices. Their Group property puts them in one group, which runs around PlaceOrder.
using System;
using PostSharp.Aspects;
using PostSharp.Aspects.Advices;
using PostSharp.Serialization;
namespace Errors.LA0132_Fixed;
[PSerializable]
public abstract class TraceAspectBase : MethodLevelAspect
{
[OnMethodEntryAdvice( Group = "Trace" )]
public void Trace( [DeclarationName] string methodName )
{
Console.WriteLine( $"Entering {methodName}." );
}
}
[PSerializable]
public class TraceAttribute : TraceAspectBase
{
// The advice method of the derived class has a name of its own. The Group property puts the advice in the group of the base class,
// because PostSharp groups the advices of each class separately.
[OnMethodExitAdvice( Group = "Trace" )]
public void TraceExit( [DeclarationName] string methodName )
{
Console.WriteLine( $"Leaving {methodName}." );
}
}
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
MethodLevelAspect
Group
AdviceDependencyAttribute
Other Resources