| Severity | Warning |
| Reported by | PostSharp.Patterns.Model |
| Message | NotifyPropertyChangedAttribute: automatic analysis of property <Property> failed. Method <Method> depends on property <OtherProperty> of type <OtherType>, but neither this type nor any type derived from it implements the INotifyPropertyChanged interface or has the [NotifyPropertyChanged] aspect. To solve this message, add the [NotifyPropertyChanged] aspect to the <OtherType> type or make it implement the INotifyPropertyChanged interface. To ignore this message, add the [SafeForDependencyAnalysis] attribute to the property <Property>. |
Cause
The NotifyPropertyChangedAttribute aspect analyzes the getter of each property to find what the property depends on. A dependency on a property of another object, in the form this.Address.City, is supported: the aspect subscribes to the PropertyChanged event of that object and raises the notification of the dependent property when City changes. This works only when the other object notifies its changes.
PostSharp reports DOM023 when <OtherType>, the type that declares <OtherProperty>, does not implement INotifyPropertyChanged and does not have the NotifyPropertyChangedAttribute aspect, and no type derived from it does. <Method> is the getter of <Property>, or a method of the same class that the getter calls. The message is a warning: the build succeeds, but the dependent property is not notified when the other property changes.
In the following example, the City property of Customer reads the City property of Address, which does not notify its changes.
using System;
using System.ComponentModel;
using PostSharp.Patterns.Model;
namespace Errors.DOM023_Error;
// Address does not notify the changes of its properties.
public class Address
{
public string City { get; set; } = "Prague";
}
[NotifyPropertyChanged]
public class Customer
{
public Address Address { get; } = new Address();
public string City => this.Address.City;
}
public static class Program
{
public static void Main()
{
var customer = new Customer();
((INotifyPropertyChanged) customer).PropertyChanged +=
( _, e ) => Console.WriteLine( $"PropertyChanged: {e.PropertyName}" );
customer.Address.City = "Brno";
Console.WriteLine( $"City: {customer.City}" );
}
}
warning DOM023: DOM023_Error.cs(18,19): NotifyPropertyChangedAttribute: automatic analysis of property Errors.DOM023_Error.Customer.City failed. Method Errors.DOM023_Error.Customer.get_City depends on property Errors.DOM023_Error.Address.City of type Errors.DOM023_Error.Address, but neither this type nor any type derived from it implements the INotifyPropertyChanged interface or has the [NotifyPropertyChanged] aspect. To solve this message, add the [NotifyPropertyChanged] aspect to the Errors.DOM023_Error.Address type or make it implement the INotifyPropertyChanged interface. To ignore this message, add the [SafeForDependencyAnalysis] attribute to the property Errors.DOM023_Error.Customer.City.
The program runs, but changing the city of the address raises no notification for Customer.City:
City: Brno
Resolution
- Add the NotifyPropertyChangedAttribute aspect to
<OtherType>, or implement INotifyPropertyChanged in it. - When the value of
<OtherProperty>never changes, or when you do not need the notification, add SafeForDependencyAnalysisAttribute to<Property>to remove the warning.
In the following example, Address has the aspect, so changing its city notifies Customer.City.
using System;
using System.ComponentModel;
using PostSharp.Patterns.Model;
namespace Errors.DOM023_Fixed;
// Address notifies the changes of its properties.
[NotifyPropertyChanged]
public class Address
{
public string City { get; set; } = "Prague";
}
[NotifyPropertyChanged]
public class Customer
{
public Address Address { get; } = new Address();
public string City => this.Address.City;
}
public static class Program
{
public static void Main()
{
var customer = new Customer();
((INotifyPropertyChanged) customer).PropertyChanged +=
( _, e ) => Console.WriteLine( $"PropertyChanged: {e.PropertyName}" );
customer.Address.City = "Brno";
Console.WriteLine( $"City: {customer.City}" );
}
}
PropertyChanged: City
City: Brno
See Also
Reference
NotifyPropertyChangedAttribute
SafeForDependencyAnalysisAttribute
INotifyPropertyChanged
Other Resources
Implementing INotifyPropertyChanged
Handling Corner Cases of the NotifyPropertyChanged Aspect