Resharper + INotifyPropertyChanged

I’ve created a handy Resharper Live Template to make creating a class that implements INotifyPropertyChanged a little easier.  To implement it, do the following:

1.  Create the live template

  1. Click Resharper->Live Templates….
  2. On the Template Explorer dialog Live Templates tab, click New Template.
  3. Enter “nprop” as the Shortcut and “INotifyPropertyChanged property” as the Description.
  4. Copy/paste the following into the template:

    private $TYPE$ $FIELDNAME$;
    public $TYPE$ $PROPERTYNAME$
    {
        get { return $FIELDNAME$; }
        set
        {
            bool changed = value != $FIELDNAME$;
            $FIELDNAME$ = value;
            if (changed) InvokePropertyChanged(new PropertyChangedEventArgs(“$PROPERTYNAME$”));
        }
    }

  5. In the macro settings, leave “TYPE” as “choose macro”, set “FIELDNAME” to “Suggest name for a variable” and “PROPERTYNAME” to “Value of FIELDNAME with the first character in uppercase” and “not editable”.
  6. Save settings and close Live Templates windows.

2.  Using the live template

  1. Create a class that implements INotifyPropertyChanged:
  2. public class Employee : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
    }

  3. Put the cursor on the PropertyChanged event name, and use the Resharper Actions List (ALT+ENTER) to generate an event invocator:
  4. public class Employee : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        private void InvokePropertyChanged(PropertyChangedEventArgs e)
        {
            PropertyChangedEventHandler changed = PropertyChanged;
            if (changed != null) changed(this, e);
        }
    }

  5. Inside the class, rather than manually typing in a property by hand, invoke the live template by typing “nprop”, then pressing TAB.
  6. The live template will create a property with a backing field for you, and the code necessary to invoke the event:
  7. Type the TypeName of the property, press TAB, then type the field name of the property.
  8. Press ENTER when you are done, rinse, and repeat as necessary.

Hope you find it useful.  Here’s a short video of it in use: INotifyPropertyChanged Resharper Live Template video.

About these ads

2 thoughts on “Resharper + INotifyPropertyChanged

  1. FYI, there’s a ReSharper plug-in called R2P (http://activemesa.com/r2p) that supports turning an auto-property into a fully specified INPChanged/Changing proeprty with backing field. It supports both string-based and lambda notations, and lets you modify either a single property or all properties within a type.

  2. Heya i’m for the first time here. I found this board and
    I in finding It really helpful & it helped me out a lot.
    I am hoping to offer something back and aid others such as you helped me.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s