INotifyPropertyChanged Support in ReSharper 7
July 24th, 2012 by Dmitri NesterukLet’s see what’s good in ReSharper 7 in addition to support for Visual Studio 2012 and WinRT development.
Ever since the release of Windows Forms, the INotifyPropertyChanged interface has been used by developers to allow properties to notify whoever needs to know (typically, the UI) that they have changed. Support for change notification, which nowadays appears in many core (WPF, Silverlight) and third-party frameworks, has been frequently requested on the ReSharper issue tracker and, in the 7.0 release, we are happy to oblige. Here’s what we’ve got in store.
Automatic implementation
As soon as you indicate that a class implements INotifyPropertyChanged, ReSharper offers a quick-fix that not only implements the interface members, but also provides a default implementation of the method as well as the needed event.

With the interface implementation in place, you can navigate to a property and convert it to a field+property declaration that calls the notification method. The property can be an auto-property or a field-backed property provided the assignment to the backing field is the last expression in the setter.

If you have fields instead of properties, you can also invoke the Generate|Properties action, and ReSharper offers the option to generate properties matching those fields with the required change notifications:

Important note: in order to get context actions related to an implemented INotifyPropertyChanged interface to show up, code annotations need to be added to the project. You can find the default implementation in ReSharper|Options|Code Annotations. These annotations contain the definition of the [NotifyPropertyChangedInvocator] attribute, which is used to decorate the method that is responsible for firing the event. Also note that this is only necessary if you’re implementing INotifyPropertyChanged yourself, and is not required if you are inheriting from a framework class that implements it.
Tracking Symbol Names
Referring to a property by name is not the safest way of doing things, but if you have to do it, ReSharper has got your back. First, ReSharper keeps a close eye on those property names for you. Should you make a mistake in a name or, for instance, refer to a property declared private, ReSharper will complain about it:

In addition, when you decide to call OnPropertyChanged manually, ReSharper will offer a completion list for that string parameter.

Framework Support
Not all implementations of INotifyPropertyChanged are identical. Third-party application frameworks often have their preferred way of notifying on property changes, and are shipped with corresponding base classes that framework users are encouraged to inherit from. One major difference is that certain frameworks prefer to use a lambda expression instead of a string literal as a parameter to the notification method.
ReSharper 7 supports the following frameworks out of the box:
-
MVVM Light (WPF 3 and 4; Silverlight 3, 4 and 5; Windows Phone 7 and 7.1)
-
Caliburn.Micro (WPF 3 and 4; Silverlight 4 and 5; Windows Phone 7.1)
-
Prism (WPF 4; Silverlight 4)
-
Catel (WPF 3 and 4)
-
MVVMHelpers (WPF 3 and 4, only lambda expressions supported)
What this means in practical terms is that if you were to inherit from, say, Prism’s NotificationObject, when it comes to generating properties you actually get a choice of which RaisePropertyChanged method the implemented properties will call:

If we pick the lambda-driven implementation, our properties will be defined similar to the following:

And, of course, this choice will correspondingly be presented in the property-altering context actions:

As you can see, ReSharper is attuned to the specifics of the parents of the containing class, and is capable of presenting all of the available choices.
Tags: INotifyPropertyChanged, MVVM, ReSharper, ReSharper 7, Silverlight, WPF

July 24th, 2012 at 4:04 pm
What about DependencyProperty? I would say its a biggest pain to deal, as there is a lot of potential copy-paste errors arise
July 24th, 2012 at 4:05 pm
@Alex what kind of features would you like ReSharper to provide with respect to dependency properties?
July 24th, 2012 at 4:11 pm
@Alex I second Dmitri’s question. As of now, R# only provides a built-in live template to generate dependency properties
July 24th, 2012 at 4:25 pm
wuut, how come i didn’t know that.
I’m using currently the GammaXaml R# plugin (http://gjxaml.codeplex.com/). It has neat feature, that if your declaration if DependencyProperty changes it will mark those as error (type changed, or Name is changed)
But yeah, in this case, its easier just delete all and create from scratch using live template
July 24th, 2012 at 4:29 pm
I’m running R#7 Beta but can’t see the feature, is it in a newer release?
July 24th, 2012 at 5:20 pm
Nice and awaited feature. It was the main reason I started develop GammaXaml in the first place
@Corrado: you have to annotate your RaisePropertyChanged or equivalent method with the new R# [NotifyPropertyChangedInvocator] if you’re not using one of the framework listed in this blog post.
July 24th, 2012 at 5:29 pm
Hi, What about C# 5 Caller Information support?
July 24th, 2012 at 5:55 pm
@duile at the moment, ReSharper does not support this mechanism. I have created a feature request for it — please feel free to vote for it.
July 24th, 2012 at 8:16 pm
Thanks @Dimitri. I have voted it.
July 25th, 2012 at 2:52 pm
How about support for [CallerMemberName]?
Your very own Jesse Liberty blogged about this:
http://jesseliberty.com/2012/06/28/c-5making-inotifypropertychanged-easier/?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed%3A+JesseLiberty-SilverlightGeek+%28Jesse+Liberty+-+Silverlight+Geek%29
July 25th, 2012 at 2:59 pm
@SledgeNZ
Please feel free to create a feature request on this.
Speaking of Jesse Liberty, he’s not really our own )
July 25th, 2012 at 5:19 pm
How easy will it be to implement a plug-in or something to use our own implementation of NotifyPropertyChanged with Resharper? In CODE Framework (my company’s framework, in the interest of full disclosure), you inherit from a “ViewModel” base class, then do something like the following:
private string _someString;
public string SomeString
{
get { return _someString; }
set
{
_someString = value;
NotifyChanged(”SomeString”);
}
}
Will Resharper pick up on the fact that “NotifyChanged” is the implementation of INotifyChanged, instead of “OnPropertyChanged”? Will there be some sort of configuration available for this?
July 25th, 2012 at 7:07 pm
@Joe: see my previous comment, you just have to annotate NotifyChanged with the attribute [NotifyPropertyChangedInvocator] from JetBrains.Annotations to make R# recognize your method.
July 26th, 2012 at 10:16 pm
Great! Thanks!
August 8th, 2012 at 1:35 am
How do you enable this feature? I don’t get that option on my resharper 7!
August 8th, 2012 at 12:20 pm
@Jason: you need to annotate the event-firing method with the
[NotifyPropertyChangedInvocator]attribute. You can get this attribute by pasting code annotations into your project – see ReSharper|Options|Code Annotations.August 16th, 2012 at 1:44 am
I’m not seeing any of this support in VS2010 + Resharper 7. Is this a VS2012 only feature?
August 20th, 2012 at 2:17 am
@snort, this feature not only VS2012 feature, it works even in VS2005. Looks like you missing R# code annotation on event-firing method.
August 20th, 2012 at 2:19 am
@SledgeNZ, [CallerMemberName] support is already implemented and will be publicly available in next minor release. Thx for your feedback!
September 4th, 2012 at 9:50 pm
For Catel developers we also provide a R# plugin http://catelresharper.codeplex.com/
September 17th, 2012 at 1:39 pm
Nice feature, but you should really just point everyone to use NotifyPropertyWeaver. Free, simple, and results in significantly cleaner code. Once you’ve tried it you’ll never go back!
September 24th, 2012 at 10:53 am
I already annotated my caller method with the [NotifyPropertyChangedInvocator] attribute, but i still can’t make this work…
What am i doing wrong?
Where’s my method:
[NotifyPropertyChangedInvocator]
protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = this.PropertyChanged;
if(handler != null)
handler(this, new PropertyChangedEventArgs(propertyName));
}
September 24th, 2012 at 11:00 am
Nevermind. It was there, but instead of a combobox it was a check button XD
May 8th, 2013 at 4:44 pm
I’m using Resharper 7 and MVVM Light. When I use class ViewModelBase in which its super class is ObservableObject which implements INotifyPropertyChanged, so therefore in my view model its inherited the implementation and events.So I should be able to alt+insert to insert properties that implement INotifyPropertyChanged, currently I dont seem to be able to and I’m having to do this manually. What am I doing wrong?
Thanks