| Severity | Error |
| Reported by | PostSharp |
| Message | Location "<Location>" is of type "<LocationType>", which cannot be matched to any ValidateValue method of aspect "<AspectType>". |
Cause
A validation aspect checks the value of a parameter, a return value, a property or a field with a ValidateValue method. An aspect has one ValidateValue method for each ILocationValidationAspect<T> interface that it implements, or one for each method that has the LocationValidationAdvice attribute. PostSharp chooses the method whose value parameter accepts the type of the location.
PostSharp reports LA0121 when no method accepts it. <LocationType> is the type of the location <Location>. For the return value of an async method, it is the type of the result of the task, and PostSharp also reports LA0121 for the return value of a void method, which has no value to validate.
In the following example, the [Positive] aspect implements ILocationValidationAspect<int> only, but it is applied to a decimal parameter.
using System;
using PostSharp.Aspects;
using PostSharp.Reflection;
using PostSharp.Serialization;
namespace Errors.LA0121_Error;
// Checks that a value is greater than zero. The aspect validates only int values.
[PSerializable]
public sealed class PositiveAttribute : LocationLevelAspect, ILocationValidationAspect<int>
{
public Exception? ValidateValue( int value, string locationName, LocationKind locationKind, LocationValidationContext context )
=> value > 0 ? null : new ArgumentOutOfRangeException( locationName, $"The {locationName} must be positive." );
}
public class OrderService
{
public void PlaceOrder( [Positive] int quantity )
{
Console.WriteLine( $"Order placed for {quantity} items." );
}
// The parameter is a decimal, but the aspect has no ValidateValue method for this type.
public void Refund( [Positive] decimal amount )
{
Console.WriteLine( $"Refunded {amount:0.00}." );
}
}
public static class Program
{
public static void Main()
{
OrderService service = new();
service.PlaceOrder( 3 );
service.Refund( 12.5m );
}
}
error LA0121: LA0121_Error.cs(24,44): Location "Errors.LA0121_Error.OrderService.Refund(System.Decimal)@amount" is of type "System.Decimal", which cannot be matched to any ValidateValue method of aspect "Errors.LA0121_Error.PositiveAttribute".
Resolution
- Implement ILocationValidationAspect<T> for the type of the location, or add a
ValidateValueadvice method for this type. - Otherwise, remove the aspect from the location, or use an aspect made for this type.
In the following example, the aspect also implements ILocationValidationAspect<decimal>.
using System;
using PostSharp.Aspects;
using PostSharp.Reflection;
using PostSharp.Serialization;
namespace Errors.LA0121_Fixed;
// Checks that a value is greater than zero. The aspect validates int and decimal values.
[PSerializable]
public sealed class PositiveAttribute : LocationLevelAspect, ILocationValidationAspect<int>, ILocationValidationAspect<decimal>
{
public Exception? ValidateValue( int value, string locationName, LocationKind locationKind, LocationValidationContext context )
=> value > 0 ? null : new ArgumentOutOfRangeException( locationName, $"The {locationName} must be positive." );
// The aspect implements the interface for decimal values too.
public Exception? ValidateValue( decimal value, string locationName, LocationKind locationKind, LocationValidationContext context )
=> value > 0 ? null : new ArgumentOutOfRangeException( locationName, $"The {locationName} must be positive." );
}
public class OrderService
{
public void PlaceOrder( [Positive] int quantity )
{
Console.WriteLine( $"Order placed for {quantity} items." );
}
public void Refund( [Positive] decimal amount )
{
Console.WriteLine( $"Refunded {amount:0.00}." );
}
}
public static class Program
{
public static void Main()
{
OrderService service = new();
service.PlaceOrder( 3 );
service.Refund( 12.5m );
try
{
service.Refund( -1m );
}
catch ( ArgumentOutOfRangeException e )
{
Console.WriteLine( $"Rejected: {e.ParamName}." );
}
}
}
Order placed for 3 items.
Refunded 12.50.
Rejected: amount.
See Also
Reference
ILocationValidationAspect<T>
LocationValidationAdvice
Other Resources