| Severity | Error |
| Reported by | PostSharp |
| Message | The advice "<Advice>" has a MatchPointcut with IsRequired = true, but it applies to no method of "<TargetType>": no method is named "<MethodName>", or no method with this name has a signature that matches the parameters of the advice. |
Cause
A MatchPointcut selects the methods of the target type that have a given name, and skips silently the methods whose signature does not match the parameters of the advice. When IsRequired is true, PostSharp reports LA0285 for each aspect instance whose advice applies to no method of <TargetType>:
- no method that
<TargetType>declares is named<MethodName>, for instance after the method is renamed. Inherited methods are not selected; - or no method with this name matches the advice: an argument is missing or has an incompatible type, or, when MatchParameterCount is
true, the number of parameters differs.
PostSharp reports the error on the target type.
In the following example, the Withdraw method of Account was renamed Debit, so the advice no longer validates the amount.
using System;
using PostSharp.Aspects;
using PostSharp.Aspects.Advices;
using PostSharp.Serialization;
namespace Errors.LA0285_Error;
[PSerializable]
public sealed class PositiveAmountAttribute : TypeLevelAspect
{
[OnMethodEntryAdvice, MatchPointcut( "Withdraw", IsRequired = true )]
public static void OnWithdraw( [Argument( 0 )] decimal amount )
{
if ( amount <= 0 )
throw new ArgumentOutOfRangeException( nameof(amount), "The amount must be positive." );
}
}
[PositiveAmount]
public class Account // The Withdraw method was renamed Debit, so the advice applies to no method.
{
public decimal Balance { get; private set; } = 100m;
public void Debit( decimal amount ) => this.Balance -= amount;
}
public static class Program
{
public static void Main()
{
Account account = new Account();
account.Debit( -50m );
Console.WriteLine( $"Balance: {account.Balance}" );
}
}
error LA0285: LA0285_Error.cs(20,14): The advice "Errors.LA0285_Error.PositiveAmountAttribute.OnWithdraw(System.Decimal)" has a MatchPointcut with IsRequired = true, but it applies to no method of "Errors.LA0285_Error.Account": no method is named "Withdraw", or no method with this name has a signature that matches the parameters of the advice.
Resolution
- Use the name of the method that the target type declares in the pointcut, or rename the method back.
- Change the parameters of the advice so that they match the signature of the method.
- When the target type may legitimately lack the method, set IsRequired to
false, which is the default value, or do not apply the aspect to this type.
In the following example, the pointcut names the Debit method, so the advice rejects the negative amount.
using System;
using PostSharp.Aspects;
using PostSharp.Aspects.Advices;
using PostSharp.Serialization;
namespace Errors.LA0285_Fixed;
[PSerializable]
public sealed class PositiveAmountAttribute : TypeLevelAspect
{
// The pointcut names the method that the target class declares.
[OnMethodEntryAdvice, MatchPointcut( "Debit", IsRequired = true )]
public static void OnWithdraw( [Argument( 0 )] decimal amount )
{
if ( amount <= 0 )
throw new ArgumentOutOfRangeException( nameof(amount), "The amount must be positive." );
}
}
[PositiveAmount]
public class Account
{
public decimal Balance { get; private set; } = 100m;
public void Debit( decimal amount ) => this.Balance -= amount;
}
public static class Program
{
public static void Main()
{
Account account = new Account();
try
{
account.Debit( -50m );
}
catch ( ArgumentOutOfRangeException e )
{
Console.WriteLine( e.Message );
}
Console.WriteLine( $"Balance: {account.Balance}" );
}
}
The amount must be positive. (Parameter 'amount')
Balance: 100
See Also
Reference
Other Resources