Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is [NotifyPropertyChangedInvocator] in C# when implements INotifyPropertyChanged?

I see two types of implementation of INotifyPropertyChanged

  • The first one:

    public abstract class ViewModelBase : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
    
        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
    
  • The second one:

    public abstract class ViewModelBase : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
    
        [NotifyPropertyChangedInvocator]
        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
    

In 2nd one you see there is an extra attribute [NotifyPropertyChangedInvocator] on the method OnPropertyChanged

In my case both behaves same but what, why and when to use this [NotifyPropertyChangedInvocator], what are benefits of this? I've searched on internet but couldn't find any good answer.

like image 743
Waqas Idrees Avatar asked Apr 22 '14 07:04

Waqas Idrees


People also ask

Why is INotifyPropertyChanged used?

The INotifyPropertyChanged interface is used to notify clients, typically binding clients, that a property value has changed. For example, consider a Person object with a property called FirstName .

What is INotifyPropertyChanged in xamarin forms?

The INotifyPropertyChanged changed interface is at the heart of XAML apps and has been a part of the . NET ecosystem since the early days of Windows Forms. The PropertyChanged event notifies the UI that a property in the binding source (usually the ViewModel) has changed. It allows the UI to update accordingly.


2 Answers

It is a Resharper attribute from their Annotations - designed to give you warning then your code looks suspicious :)
Consider this:

public class Foo : INotifyPropertyChanged 
{
     public event PropertyChangedEventHandler PropertyChanged;
     
     [NotifyPropertyChangedInvocator]
     protected virtual void NotifyChanged(string propertyName) { ... }
  
     private string _name;
     public string Name {
       get { return _name; }
       set { 
             _name = value; 
             NotifyChanged("LastName");//<-- warning here
           } 
     }
   }

With the [NotifyPropertyChangedInvocator] attribute on the NotifyChanged method Resharper will give you a warning, that you are invoking the method with a (presumably) wrong value.

Because Resharper now knows that method should be called to make change notification, it will help you convert normal properties into properties with change notification: Visual Studio Screenshot showing ReShaper suggestions
Converting it into this:

public string Name
{
  get { return _name; }
  set
  {
    if (value == _name) return;
    _name = value;
    NotifyChange("Name");
  }
}



This example is from the documentation on the [NotifyPropertyChangedInvocator] attribute found here:
enter image description here

like image 191
Jens Kloster Avatar answered Oct 11 '22 15:10

Jens Kloster


The NotifyPropertyChangedInvocator is a Resharper feature.

You can simply remove it from your code in order for it to work

Similar question been asked here:

does anyone know how to get the [NotifyPropertyChangedInvocator]

like image 4
Sajeetharan Avatar answered Oct 11 '22 15:10

Sajeetharan