| Severity | Error |
| Reported by | PostSharp.Patterns.Xaml |
| Message | Error in the [<Aspect>] aspect applied to property <Property>: the validation method <Method> does not have a correct signature. Correct signatures are "bool MyValidateValueHandler(DependencyProperty property, <DeclaringType> instance, <ValueType> value)" or "bool MyValidateValueHandler<T>(DependencyProperty property, <DeclaringType> instance, T value)" for static methods and "bool MyValidateValueHandler(DependencyProperty property, <ValueType> value)" or "bool MyValidateValueHandler<T>(DependencyProperty property, T value)" for instance methods. The generic argument should be assignable from the property type. Parameters "property" and "instance" are optional and may be omitted. |
Cause
The DependencyPropertyAttribute and AttachedPropertyAttribute aspects call a validation method before they change the value of the property. They find the method by its name: Validate followed by the name of the property, or the name that you set in the <xref:PostSharp.Patterns.Xaml.DependencyPropertyBaseAttribute.ValidateValueMethod> property of the aspect. PostSharp reports XAM055 when the parameters of this method do not match one of the supported signatures:
- A static method takes the value, optionally preceded by the DependencyProperty and by the instance, in this order. The instance parameter is of the type that declares the property, DependencyObject or
object. - An instance method takes the value, optionally preceded by the DependencyProperty.
- The value parameter is of a type assignable from the type of the property, or is the generic parameter of a generic method. No parameter can be
out.
The method returns bool: true accepts the value, and false makes the setter throw an ArgumentException. PostSharp does not check the return type at build time.
<DeclaringType> and <ValueType> are the short names of the class and of the type of the property, such as Int32. For an attached property, the current version of the message shows Attached`1 as <ValueType> instead of the type of the value. PostSharp reports the message on the property.
In the following example, the ValidateQuantity method takes a string, but the Quantity property is an int.
using System;
using System.Windows;
using PostSharp.Patterns.Xaml;
namespace Errors.XAM055_Error;
public class OrderLine : DependencyObject
{
[DependencyProperty]
public int Quantity { get; set; }
// The parameter of the validation method must accept an int, the type of the property.
public bool ValidateQuantity( string value ) => value.Length > 0;
}
public static class Program
{
public static void Main()
{
var line = new OrderLine();
line.Quantity = 5;
Console.WriteLine( $"Quantity: {line.Quantity}" );
try
{
line.Quantity = -1;
}
catch ( ArgumentException e )
{
Console.WriteLine( $"Rejected: {e.Message}" );
}
}
}
error XAM055: XAM055_Error.cs(10,16): Error in the [DependencyProperty] aspect applied to property Errors.XAM055_Error.OrderLine.Quantity: the validation method Errors.XAM055_Error.OrderLine.ValidateQuantity(System.String) does not have a correct signature. Correct signatures are "bool MyValidateValueHandler(DependencyProperty property, OrderLine instance, Int32 value)" or "bool MyValidateValueHandler<T>(DependencyProperty property, OrderLine instance, T value)" for static methods and "bool MyValidateValueHandler(DependencyProperty property, Int32 value)" or "bool MyValidateValueHandler<T>(DependencyProperty property, T value)" for instance methods. The generic argument should be assignable from the property type. Parameters "property" and "instance" are optional and may be omitted.
Resolution
Change the parameters of the validation method to one of the supported signatures, with a value parameter of the type of the property, or make the method generic. When the method has the name of the convention but is not meant to validate the property, rename it, or set <xref:PostSharp.Patterns.Xaml.DependencyPropertyBaseAttribute.ValidateValueMethod> to the name of another method.
In the following example, ValidateQuantity takes an int and rejects the values that are not positive.
using System;
using System.Windows;
using PostSharp.Patterns.Xaml;
namespace Errors.XAM055_Fixed;
public class OrderLine : DependencyObject
{
[DependencyProperty]
public int Quantity { get; set; }
// The validation method returns bool and takes a value of the type of the property.
public bool ValidateQuantity( int value ) => value > 0;
}
public static class Program
{
public static void Main()
{
var line = new OrderLine();
line.Quantity = 5;
Console.WriteLine( $"Quantity: {line.Quantity}" );
try
{
line.Quantity = -1;
}
catch ( ArgumentException e )
{
Console.WriteLine( $"Rejected: {e.Message}" );
}
}
}
Quantity: 5
Rejected: Invalid property value. (Parameter 'value')
See Also
Reference
DependencyPropertyAttribute
AttachedPropertyAttribute
<xref:PostSharp.Patterns.Xaml.DependencyPropertyBaseAttribute.ValidateValueMethod>
Other Resources