| Severity | Error |
| Reported by | PostSharp.Patterns.Xaml |
| Message | Error in the [<Aspect>] aspect applied to property <Property>: type <DeclaringType> does not declare any method named "<Method>". |
Cause
The DependencyPropertyAttribute and AttachedPropertyAttribute aspects can validate the values assigned to the property with a method of the class. By default, they look for a method named ValidateName, where Name is the name of the property, and skip the validation when there is none. When you set the <xref:PostSharp.Patterns.Xaml.DependencyPropertyBaseAttribute.ValidateValueMethod> property, the method is required.
PostSharp reports XAM053 when the type that declares the property, <DeclaringType>, declares no method named <Method>, the value of <xref:PostSharp.Patterns.Xaml.DependencyPropertyBaseAttribute.ValidateValueMethod>. The aspect searches the methods of any visibility, static or instance, but only those that <DeclaringType> itself declares: a method inherited from a base class is not found. The usual causes are a misspelled name and a method that was renamed or moved to a base class.
In the following example, <xref:PostSharp.Patterns.Xaml.DependencyPropertyBaseAttribute.ValidateValueMethod> names IsValidPhone, but the method is named IsValidPhoneNumber.
using System;
using System.Windows;
using PostSharp.Patterns.Xaml;
namespace Errors.XAM053_Error;
public class ContactCard : DependencyObject
{
// ContactCard declares no method named IsValidPhone.
[DependencyProperty( ValidateValueMethod = "IsValidPhone" )]
public string? Phone { get; set; }
private static bool IsValidPhoneNumber( string? value ) => value == null || value.StartsWith( '+' );
}
public static class Program
{
public static void Main()
{
var card = new ContactCard { Phone = "+1 555 0100" };
Console.WriteLine( $"Phone: {card.Phone}" );
try
{
card.Phone = "555 0100";
}
catch ( ArgumentException )
{
Console.WriteLine( "Rejected a phone number without a country code." );
}
}
}
error XAM053: XAM053_Error.cs(11,20): Error in the [DependencyProperty] aspect applied to property Errors.XAM053_Error.ContactCard.Phone: type Errors.XAM053_Error.ContactCard does not declare any method named "IsValidPhone".
Resolution
Set <xref:PostSharp.Patterns.Xaml.DependencyPropertyBaseAttribute.ValidateValueMethod> to the name of a method declared in the same class as the property, preferably with the nameof operator, so that the compiler checks the name. When the validation method is in a base class, declare a method in the class of the property that calls it. The method must also have one of the signatures that Dependency Property lists, or PostSharp reports XAM055.
In the following example, nameof names the IsValidPhoneNumber method, which rejects a phone number without a country code.
using System;
using System.Windows;
using PostSharp.Patterns.Xaml;
namespace Errors.XAM053_Fixed;
public class ContactCard : DependencyObject
{
// ValidateValueMethod names a method that ContactCard declares.
[DependencyProperty( ValidateValueMethod = nameof(IsValidPhoneNumber) )]
public string? Phone { get; set; }
private static bool IsValidPhoneNumber( string? value ) => value == null || value.StartsWith( '+' );
}
public static class Program
{
public static void Main()
{
var card = new ContactCard { Phone = "+1 555 0100" };
Console.WriteLine( $"Phone: {card.Phone}" );
try
{
card.Phone = "555 0100";
}
catch ( ArgumentException )
{
Console.WriteLine( "Rejected a phone number without a country code." );
}
}
}
Phone: +1 555 0100
Rejected a phone number without a country code.
See Also
Reference
<xref:PostSharp.Patterns.Xaml.DependencyPropertyBaseAttribute.ValidateValueMethod>
DependencyPropertyAttribute
AttachedPropertyAttribute
Other Resources