52

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.

Jason Evans
  • 28,906
  • 14
  • 90
  • 154
Waqas Idrees
  • 1,443
  • 2
  • 17
  • 36

2 Answers2

93

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

xa0082249956
  • 315
  • 5
  • 6
Jens Kloster
  • 11,099
  • 5
  • 40
  • 54
  • 13
    Plus 1 for the explanation, and not just answering with a link. This should be selected as the answer. – user3791372 Mar 07 '15 at 02:11
  • I do not understand.. What warning is given, why, when, what for? Is this normal it changed from "LastName" to "Name"? I'm confused. (Also link is dead now - and the page isn't on archive.org) – jeromej Apr 12 '19 at 12:33
  • @JeromeJ I have updated the link. The warning is something resharper does (underlining the code with "zigzag" blue color). For more info on _why_ the above property is problematic - search for `INotifyPropertyChanged` and examples of how it is usually implemented – Jens Kloster Apr 12 '19 at 16:24
  • If you have multiple methods having this tag, ReSharper will list them in the contextmenu and let you pick the one you want to auto-implement. (eg one using `string` or one using `Expression>` ) – jeromej Jun 18 '20 at 13:29
5

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]

Sajeetharan
  • 216,225
  • 63
  • 350
  • 396
  • Is is a C# Feature. Resharper is just an Tool, and not able to add any extra code which c# is not Supported. There is also some thing behind NotifyPropertyChangedInvocator. – Waqas Idrees Apr 22 '14 at 07:32
  • Then You say [CallerMemberName] is also added by Resharper, By removing the Code is also working, But CallMemberName resolve a headache of giving name or properties where we call it. Same i want to know about NotifyPropertyChangedInvocator Check it here http://www.kunal-chowdhury.com/2012/07/whats-new-in-c-50-learn-about.html – Waqas Idrees Apr 22 '14 at 07:35
  • @WaqasIdrees CallMemberName is a feature of C# with .net 4.5.Allows you to obtain the method or property name of the caller to the method. Can you prove NotifyPropertyChangedInvocator comes with C#? since you have downvoted – Sajeetharan Apr 22 '14 at 07:40
  • But as you says that NotifyPropertyChangedInvocator is a Resharper feature that's why i down voted. It is a c# Feature. But what is the benefits of this and when to use. Why Resharper adding this. – Waqas Idrees Apr 22 '14 at 07:43
  • 5
    @WaqasIdrees can you post the link or documentation for NotifyPropertyChangedInvocator to prove it comes with C# – Sajeetharan Apr 22 '14 at 07:50
  • 3
    +0, -1 For [plagiarizing](http://social.msdn.microsoft.com/Forums/vstudio/en-US/5c000063-517a-4193-b7f7-29dadd7cbd4b/c-wpf-app-does-anyone-know-how-to-get-the-notifypropertychangedinvocator-statement-to-work?forum=wpf) – Sriram Sakthivel Apr 22 '14 at 08:55
  • @SriramSakthivel I have posted the link with the answer – Sajeetharan Apr 22 '14 at 09:18
  • That is not the link which I provided, Your link is also a plagiarized! – Sriram Sakthivel Apr 22 '14 at 09:20
  • @SriramSakthivel Should i remove the answer? – Sajeetharan Apr 22 '14 at 09:22
  • Not necessarily, Just give the credit to the original answer. – Sriram Sakthivel Apr 22 '14 at 09:25
  • @WaqasIdrees - `INotifyPropertyChanged` is defined in a .NET library (it's not C# language feature). `NotifyPropertyChangedInvocator` is a ReSharper feature. [Search for "NotifyPropertyChangedInvocator" on MSDN](http://social.msdn.microsoft.com/Search/en-US?query=NotifyPropertyChangedInvocator) and you'll find that there is no documentation for it, only links to forums and Stackoverflow. – Richard Szalay Apr 22 '14 at 09:35
  • 1
    Simply stating that you can remove `NotifyPropertyChangedInvocator` from your code 'in order for it to work' doesn't come close to answering this question. – ProfK Dec 06 '14 at 13:50