Open sandboxFocusImprove this doc

DOM030: IgnoreAutoChangeNotification Applied to a Method

Severity Warning
Reported by PostSharp.Patterns.Model
Message Usage of <Attribute> on "<Method>" is deprecated. <Reason> This warning may become an error in a future version.

Cause

The IgnoreAutoChangeNotificationAttribute attribute, applied to a property, prevents the NotifyPropertyChangedAttribute aspect from raising the change notifications of this property. For historical reasons, the attribute can also be applied to a method. The aspect then accepts the calls to the method without analyzing them, so the properties whose getters call the method lose the dependencies that the method hides.

PostSharp reports DOM030 on each method that has the attribute. <Attribute> is [IgnoreAutoChangeNotification], and <Reason> is The attribute should not be used on methods.

In the following example, the attribute is applied to ComputeTotal, which the Total property calls.

using System;
using System.ComponentModel;
using PostSharp.Patterns.Model;

namespace Errors.DOM030_Error;

[NotifyPropertyChanged]
public class OrderLine
{
    public decimal UnitPrice { get; set; }

    public int Quantity { get; set; }

    public decimal Total => this.ComputeTotal();

    // [IgnoreAutoChangeNotification] is meant for properties, not methods.
    [IgnoreAutoChangeNotification]
    private decimal ComputeTotal() => this.UnitPrice * this.Quantity;
}

public static class Program
{
    public static void Main()
    {
        var line = new OrderLine { UnitPrice = 10m };
        ((INotifyPropertyChanged) line).PropertyChanged +=
            ( _, e ) => Console.WriteLine( $"PropertyChanged: {e.PropertyName}" );

        line.Quantity = 3;
        Console.WriteLine( $"Total: {line.Total}" );
    }
}
warning DOM030: DOM030_Error.cs(18,21): Usage of [IgnoreAutoChangeNotification] on "Errors.DOM030_Error.OrderLine.ComputeTotal" is deprecated. The attribute should not be used on methods. This warning may become an error in a future version.

The program runs, but changing Quantity raises no notification for Total:

PropertyChanged: Quantity
Total: 30

Resolution

Remove the attribute from the method. Then, depending on what the method does:

  • When the method belongs to the same class, the aspect analyzes it and finds its dependencies.
  • When the method depends only on its arguments, mark it with PureAttribute.
  • Otherwise, add SafeForDependencyAnalysisAttribute to the properties that call the method, and declare their dependencies with On.

In the following example, the attribute is removed, and changing Quantity notifies Total.

using System;
using System.ComponentModel;
using PostSharp.Patterns.Model;

namespace Errors.DOM030_Fixed;

[NotifyPropertyChanged]
public class OrderLine
{
    public decimal UnitPrice { get; set; }

    public int Quantity { get; set; }

    public decimal Total => this.ComputeTotal();

    // Without the attribute, the aspect analyzes the method and finds its dependencies.
    private decimal ComputeTotal() => this.UnitPrice * this.Quantity;
}

public static class Program
{
    public static void Main()
    {
        var line = new OrderLine { UnitPrice = 10m };
        ((INotifyPropertyChanged) line).PropertyChanged +=
            ( _, e ) => Console.WriteLine( $"PropertyChanged: {e.PropertyName}" );

        line.Quantity = 3;
        Console.WriteLine( $"Total: {line.Total}" );
    }
}
PropertyChanged: Quantity
PropertyChanged: Total
Total: 30

See Also

Reference

IgnoreAutoChangeNotificationAttribute
NotifyPropertyChangedAttribute
PureAttribute
SafeForDependencyAnalysisAttribute

Other Resources

Handling Corner Cases of the NotifyPropertyChanged Aspect