| Severity | Error |
| Reported by | PostSharp |
| Message | Cannot find a weaver for the advice of type "<AdviceType>". Use one of the advice types of PostSharp instead of a custom type. |
Cause
An advice is a member of an aspect class that PostSharp weaves into the target code. PostSharp knows how to weave each advice type that it defines, such as OnMethodEntryAdvice or IntroduceInterfaceAdviceInstance. The base classes Advice and AdviceInstance are public, but deriving from them does not create a new kind of advice: PostSharp has no weaver for the derived type, and reports LA0047.
PostSharp reports the message in two cases:
- A member of the aspect class has a custom attribute derived from Advice. The message is reported on this member.
- The ProvideAdvices method of the aspect returns an object of a custom class derived from AdviceInstance. The message is reported on the aspect class.
<AdviceType> is the custom advice type.
In the following example, the OnEntry method of the [Trace] aspect has a custom TraceEntryAdvice attribute.
using System;
using PostSharp.Aspects;
using PostSharp.Aspects.Advices;
using PostSharp.Extensibility;
using PostSharp.Serialization;
namespace Errors.LA0047_Error;
// A custom advice type. PostSharp only knows how to weave the advice types that it defines.
[AttributeUsage( AttributeTargets.Method )]
public sealed class TraceEntryAdvice : Advice
{
}
[PSerializable]
public class TraceAttribute : TypeLevelAspect
{
// PostSharp has no weaver for TraceEntryAdvice.
[TraceEntryAdvice]
[MulticastPointcut( Targets = MulticastTargets.Method, Attributes = MulticastAttributes.Public | MulticastAttributes.Instance )]
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 LA0047: LA0047_Error.cs(21,17): Cannot find a weaver for the advice of type "Errors.LA0047_Error.TraceEntryAdvice". Use one of the advice types of PostSharp instead of a custom type.
Resolution
Use an advice type of PostSharp. The behavior that you meant to add belongs in the method of the advice, not in its type: for example, OnMethodEntryAdvice runs the method of the aspect before each method that its pointcut selects. To provide advices from code, return the advice instances of PostSharp from ProvideAdvices.
In the following example, the OnEntry method of the aspect is an OnMethodEntryAdvice.
using System;
using PostSharp.Aspects;
using PostSharp.Aspects.Advices;
using PostSharp.Extensibility;
using PostSharp.Serialization;
namespace Errors.LA0047_Fixed;
[PSerializable]
public class TraceAttribute : TypeLevelAspect
{
// OnMethodEntryAdvice is an advice type of PostSharp.
[OnMethodEntryAdvice]
[MulticastPointcut( Targets = MulticastTargets.Method, Attributes = MulticastAttributes.Public | MulticastAttributes.Instance )]
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
Advice
AdviceInstance
IAdviceProvider
Other Resources
Developing Type- and Assembly-Level Aspects
Providing Advices Dynamically