| 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 call a change handler method after the value of the property changes. By default, they look for an optional method named On followed by the name of the property and Changed, such as OnQuantityChanged. When you set the <xref:PostSharp.Patterns.Xaml.DependencyPropertyBaseAttribute.PropertyChangedMethod> property of the aspect, the aspect looks for a method with exactly this name instead, and reports XAM057 when the class does not have one.
The aspect searches the methods of any visibility, static or instance, but only those that the class of the property itself declares: a method inherited from a base class is not found. PostSharp reports the message on the property.
The current version of the message says that the method "is not accessible", which is misleading: the class does not declare the method, and the aspect never checks accessibility.
In the following example, <xref:PostSharp.Patterns.Xaml.DependencyPropertyBaseAttribute.PropertyChangedMethod> names a RecalculateTotals method, but the method is named RecalculateTotal.
using System;
using System.Windows;
using PostSharp.Patterns.Xaml;
namespace Errors.XAM057_Error;
public class OrderLine : DependencyObject
{
public const decimal UnitPrice = 12.5m;
// The class declares no method named RecalculateTotals.
[DependencyProperty( PropertyChangedMethod = "RecalculateTotals" )]
public int Quantity { get; set; }
private void RecalculateTotal() => Console.WriteLine( $"Total: {this.Quantity * UnitPrice}" );
}
public static class Program
{
public static void Main()
{
var line = new OrderLine();
line.Quantity = 2;
line.Quantity = 3;
}
}
error XAM057: XAM057_Error.cs(13,16): Error in the [DependencyProperty] aspect applied to property Errors.XAM057_Error.OrderLine.Quantity: type Errors.XAM057_Error.OrderLine does not declare any method named "RecalculateTotals".
Resolution
- Correct the name in <xref:PostSharp.Patterns.Xaml.DependencyPropertyBaseAttribute.PropertyChangedMethod>. Use the
nameofoperator rather than a string literal, so that the C# compiler checks the name and a rename updates it. - When the method is declared in a base class, declare it in the class of the property, for example as a method that calls the method of the base class.
- Remove <xref:PostSharp.Patterns.Xaml.DependencyPropertyBaseAttribute.PropertyChangedMethod> and name the method by the convention, such as
OnQuantityChanged.
In the following example, the aspect names the method with nameof.
using System;
using System.Windows;
using PostSharp.Patterns.Xaml;
namespace Errors.XAM057_Fixed;
public class OrderLine : DependencyObject
{
public const decimal UnitPrice = 12.5m;
// The nameof operator makes the compiler check the name of the method.
[DependencyProperty( PropertyChangedMethod = nameof( RecalculateTotal ) )]
public int Quantity { get; set; }
private void RecalculateTotal() => Console.WriteLine( $"Total: {this.Quantity * UnitPrice}" );
}
public static class Program
{
public static void Main()
{
var line = new OrderLine();
line.Quantity = 2;
line.Quantity = 3;
}
}
Total: 25.0
Total: 37.5
See Also
Reference
DependencyPropertyAttribute
AttachedPropertyAttribute
<xref:PostSharp.Patterns.Xaml.DependencyPropertyBaseAttribute.PropertyChangedMethod>
Other Resources