INotifyPropertyChanged Support in ReSharper 7

Let’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.

This entry was posted in ReSharper Tips&Tricks and tagged , , , , , . Bookmark the permalink.

26 Responses to INotifyPropertyChanged Support in ReSharper 7

  1. Alex says:

    What about DependencyProperty? I would say its a biggest pain to deal, as there is a lot of potential copy-paste errors arise :)

  2. Dmitri Nesteruk says:

    @Alex what kind of features would you like ReSharper to provide with respect to dependency properties?

  3. @Alex I second Dmitri’s question. As of now, R# only provides a built-in live template to generate dependency properties

  4. Alex says:

    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 :)

  5. Corrado Cavalli says:

    I’m running R#7 Beta but can’t see the feature, is it in a newer release?

  6. Julien Lebosquain says:

    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.

  7. duile says:

    Hi, What about C# 5 Caller Information support?

  8. Dmitri Nesteruk says:

    @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.

  9. duile says:

    Thanks @Dimitri. I have voted it.

  10. @SledgeNZ
    Please feel free to create a feature request on this.

    Speaking of Jesse Liberty, he’s not really our own )

  11. Joe Reynolds says:

    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?

  12. Julien Lebosquain says:

    @Joe: see my previous comment, you just have to annotate NotifyChanged with the attribute [NotifyPropertyChangedInvocator] from JetBrains.Annotations to make R# recognize your method.

  13. Joe Reynolds says:

    Great! Thanks!

  14. Jason says:

    How do you enable this feature? I don’t get that option on my resharper 7!

  15. Dmitri Nesteruk says:

    @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.

  16. snort says:

    I’m not seeing any of this support in VS2010 + Resharper 7. Is this a VS2012 only feature?

  17. controlflow says:

    @snort, this feature not only VS2012 feature, it works even in VS2005. Looks like you missing R# code annotation on event-firing method.

  18. controlflow says:

    @SledgeNZ, [CallerMemberName] support is already implemented and will be publicly available in next minor release. Thx for your feedback!

  19. alexfdezsauco says:

    For Catel developers we also provide a R# plugin http://catelresharper.codeplex.com/

  20. Marcus says:

    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!

  21. Lukas says:

    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));
    }

  22. Lukas says:

    Nevermind. It was there, but instead of a combobox it was a check button XD

  23. elmanaw says:

    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

  24. xoofx says:

    @elmanaw I have the exact same problem. Created an issue here http://youtrack.jetbrains.com/issue/RSRP-383101

  25. for the love of everything good and right, i just wasted 90 minutes of my life figuring out this simple thing.

    move the caret/cursor to THE NAME OF THE PROPERTY, not on the accessibility modifier (public/private/protected) at the beginning of the property

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>