Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resharper template for automatic INotifyPropertyChanged implementation

Is it possible to write code template or a snippet which will do following:

I have a property declared like this:

public string String1 {get;set;}

And I want reshaprer to automatically generate following:

private string _string1;
public string String1
{
    get
    {
        return _string1;
    }

    set
    {

        if (_string1 != value)
        {
            _string1 = value;
            RaisePropertyChanged(() => String1);
        }
    }
}

Just have read the http://koder.wordpress.com/2010/03/25/resharper-inotifypropertychanged/ article and have created new live template which can insert code for a new property like I want it to be.

Is it possible to setup this template in such way, that it can appear in the Alt+Enter menu like a suggestion when cursor on the declaration of a property

Like:

{Access modifiers} **{Type} {Name}** {Possible get;set; statements}
like image 827
v00d00 Avatar asked Jul 03 '11 10:07

v00d00


3 Answers

This is the live template I came up with, I assigned 'npp' to initiate it. I don't think that you can Ctrl-Shift-R and replace it, but you can delete the old property declaration line and type 'npp' to invoke the template.

private $TYPE$ $NAME1$;
public $TYPE$ $NAME2$
{
    get
    {
        return $NAME1$;
    }

    set
    {

        if ($NAME1$ == value) return;

        $NAME1$ = value;
        RaisePropertyChanged(() => $NAME1$);
    }
}
like image 125
Ritch Melton Avatar answered Oct 18 '22 17:10

Ritch Melton


I've created a slightly different custom template (ReSharper > Options > Code Inspection > Custom Patterns)

Search pattern:

public $Type$ $Pname$
{
   get { return $FName$; }
   set { $FName$ = value; }
}

Replace pattern:

public $Type$ $Pname$
{
    get { return $FName$; }
    set { $FName$ = value; NotifyOnPropertyChanged(() => $Pname$); }
}

And my workflow is the following for the already created auto-properties:

  1. Alt+Enter on property name -> To Property with backing field
  2. Alt+Enter on property name -> apply my custom template

It's not the best solution but it works for me.

like image 23
nemesv Avatar answered Oct 18 '22 18:10

nemesv


If you want this as a code inspection and a quick-fix, you can create a structural replace pattern. Go to ReSharper > Options > Code Inspection > Custom Patterns, click Add Pattern, and enter the following:

Search pattern:

public $type$ $name$ {get;set;}

where $type$ is a type placeholder of type System.Object or derived, and $name$ is an identifier placeholder.

Replace pattern:

private string _$name$;
public string $name$
    {
        get
        {
            return _$name$;
        }

        set
        {
            if (_$name$ != value)
            {
                _$name$ = value;
                RaisePropertyChanged(() => $name$);
            }
        }
    }

Set Pattern Severity to a particular severity level, which influences how ReSharper highlights matches in the text editor. Optionally, enter description for both patterns so that search pattern description focuses on the problem (i.e. "This regular property is suspicious") and replace pattern description focuses on solving the problem (i.e. "Replace with INotifyPropertyChanged implementation") Click Add, and you should be done.

Now there are two problems with this approach:

  1. You have no way to influence the casing of the $name$ placeholder - after deploying, ReSharper will most probably color the new property with "inconsistent naming" inspections all over
  2. It doesn't work on my machine :) The original property is highlighted, a quick-fix for the replace pattern is there, but it just doesn't apply it. I either need a sleep or to file a bug report. Update: decided that I need both. Here's the bug report
like image 21
Jura Gorohovsky Avatar answered Oct 18 '22 18:10

Jura Gorohovsky