| Severity | Error |
| Reported by | PostSharp.Patterns.Model |
| Message | Class "<Class>" or one of its base classes already defines the ChildPropertyChanged event or the OnChildPropertyChanged(string) method, whose names the [NotifyPropertyChanged] aspect reserves. Rename the member. |
Cause
The NotifyPropertyChangedAttribute aspect propagates the changes of child objects to their parents. For this purpose, it adds a ChildPropertyChanged event and an OnChildPropertyChanged method to the class, so these names are reserved for the aspect.
PostSharp reports DOM009 on a class with the aspect when the class, or one of its base classes, already defines an event named ChildPropertyChanged, of any type, or a method named OnChildPropertyChanged with a single string parameter. The check does not apply to a class whose base class has the aspect, because the members then come from the aspect of the base class.
In the following example, Order defines its own ChildPropertyChanged event.
using System;
using System.ComponentModel;
using PostSharp.Patterns.Model;
namespace Errors.DOM009_Error;
// The class defines a ChildPropertyChanged event, a name that the aspect reserves.
[NotifyPropertyChanged]
public class Order
{
public decimal Total { get; set; }
public event EventHandler? ChildPropertyChanged;
public void ChangeLine()
{
this.ChildPropertyChanged?.Invoke( this, EventArgs.Empty );
}
}
public static class Program
{
public static void Main()
{
Order order = new Order();
( (INotifyPropertyChanged) order ).PropertyChanged += ( _, e ) => Console.WriteLine( $"{e.PropertyName} changed." );
order.ChildPropertyChanged += ( _, _ ) => Console.WriteLine( "A line changed." );
order.Total = 100;
order.ChangeLine();
}
}
error DOM009: DOM009_Error.cs(9,14): Class "Errors.DOM009_Error.Order" or one of its base classes already defines the ChildPropertyChanged event or the OnChildPropertyChanged(string) method, whose names the [NotifyPropertyChanged] aspect reserves. Rename the member.
Resolution
Rename the event or the method, in the class or in the base class that defines it.
In the following example, the event is renamed LineChanged.
using System;
using System.ComponentModel;
using PostSharp.Patterns.Model;
namespace Errors.DOM009_Fixed;
[NotifyPropertyChanged]
public class Order
{
public decimal Total { get; set; }
// The event has a name that the aspect does not use.
public event EventHandler? LineChanged;
public void ChangeLine()
{
this.LineChanged?.Invoke( this, EventArgs.Empty );
}
}
public static class Program
{
public static void Main()
{
Order order = new Order();
( (INotifyPropertyChanged) order ).PropertyChanged += ( _, e ) => Console.WriteLine( $"{e.PropertyName} changed." );
order.LineChanged += ( _, _ ) => Console.WriteLine( "A line changed." );
order.Total = 100;
order.ChangeLine();
}
}
Total changed.
A line changed.
See Also
Reference
NotifyPropertyChangedAttribute
Other Resources
Implementing INotifyPropertyChanged
Understanding the NotifyPropertyChanged Aspect