| Severity | Error |
| Reported by | PostSharp |
| Message | Cannot find the method "<Method>" named by the MethodPointcut of the advice "<Advice>": type "<AspectType>" has no instance method of this name that takes one parameter of type Object or of the reflection type of the aspect target, and returns IEnumerable<T> of Object or of the reflection type of the advice targets. |
Cause
A MethodPointcut selects the targets of an advice with a method of the aspect class, which it names. PostSharp looks in the aspect class for a method with this name and the following signature:
- It is an instance method, not a static one.
- Its only parameter is of type object or of the reflection type of the declarations to which the aspect applies, such as Type for a TypeLevelAspect.
- It returns an IEnumerable<T> exactly, not an array or a list, of object or of the reflection type of the declarations to which the advice applies, such as MethodInfo or MethodBase for an OnMethodEntryAdvice.
When no method matches, PostSharp reports LA0101 on the advice method. PostSharp then also reports LA0049, which says that the advice has no pointcut: it ignores the pointcut that it could not resolve. Fixing LA0101 removes LA0049 too.
In the following example, the pointcut method returns an array of MethodInfo.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using PostSharp.Aspects;
using PostSharp.Aspects.Advices;
using PostSharp.Serialization;
namespace Errors.LA0101_Error;
[PSerializable]
public class LogPublicMethodsAttribute : TypeLevelAspect
{
// The pointcut method returns an array, but PostSharp looks for a method that returns IEnumerable<T>.
private MethodInfo[] SelectMethods( Type type )
=> type.GetMethods( BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly ).ToArray();
[OnMethodEntryAdvice, MethodPointcut( nameof(SelectMethods) )]
public void OnEntry( MethodExecutionArgs args )
{
Console.WriteLine( $"Entering {args.Method.Name}." );
}
}
[LogPublicMethods]
public class OrderService
{
public void PlaceOrder() => Console.WriteLine( "Order placed." );
}
public static class Program
{
public static void Main()
{
new OrderService().PlaceOrder();
}
}
error LA0101: LA0101_Error.cs(19,17): Cannot find the method "SelectMethods" named by the MethodPointcut of the advice "Errors.LA0101_Error.LogPublicMethodsAttribute.OnEntry(PostSharp.Aspects.MethodExecutionArgs)": type "Errors.LA0101_Error.LogPublicMethodsAttribute" has no instance method of this name that takes one parameter of type Object or of the reflection type of the aspect target, and returns IEnumerable<T> of Object or of the reflection type of the advice targets.
Resolution
Give the pointcut method the signature above. Check also that the name in the MethodPointcut is the name of the method, ideally with the nameof operator.
In the following example, the pointcut method returns an IEnumerable<T> of MethodInfo.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using PostSharp.Aspects;
using PostSharp.Aspects.Advices;
using PostSharp.Serialization;
namespace Errors.LA0101_Fixed;
[PSerializable]
public class LogPublicMethodsAttribute : TypeLevelAspect
{
// The pointcut method returns IEnumerable<MethodInfo>.
private IEnumerable<MethodInfo> SelectMethods( Type type )
=> type.GetMethods( BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly ).ToArray();
[OnMethodEntryAdvice, MethodPointcut( nameof(SelectMethods) )]
public void OnEntry( MethodExecutionArgs args )
{
Console.WriteLine( $"Entering {args.Method.Name}." );
}
}
[LogPublicMethods]
public class OrderService
{
public void PlaceOrder() => Console.WriteLine( "Order placed." );
}
public static class Program
{
public static void Main()
{
new OrderService().PlaceOrder();
}
}
Entering PlaceOrder.
Order placed.
See Also
Reference
MethodPointcut
TypeLevelAspect
Other Resources