14

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}
Marijn
  • 10,367
  • 5
  • 59
  • 80
v00d00
  • 3,215
  • 3
  • 32
  • 43
  • 3
    Not a direct answer, but have you tried a base class (thats what we do in my shop), or NotifyPropertyWeaver (http://code.google.com/p/notifypropertyweaver/) – Ritch Melton Jul 03 '11 at 11:11

5 Answers5

10

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$);
    }
}
Ritch Melton
  • 11,498
  • 4
  • 41
  • 54
7

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.

nemesv
  • 138,284
  • 16
  • 416
  • 359
5

Another possibility with two parameters that use a Macro for lowercasing the first letter of the private variable :

public $PropertyType$ $PropertyName$
{
    get { return _$variableName$; }

    set
    {
        if (_$variableName$ != value)
        {
            _$variableName$ = value;
            RaisePropertyChanged(() => $PropertyName$);
        }
    }
}
  1. Resharper > Template Explorer > New Template (Icon)
  2. Paste the code snippet.
  3. Click on Choose macro for the variable "variableName"
  4. Select "Value of another variable with the first character in lower"
  5. (Optional) Set as "Not editable" if you use "ALT + ENTER" for creating the field with R# (The field will be created next to your other fields. This avoid a Copy/Paste)

Here is a screenshot of the final R# Tempate : RaisePropertyChanged Resharper Template

Steven Muhr
  • 3,339
  • 28
  • 46
5

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
Jura Gorohovsky
  • 9,886
  • 40
  • 46
3

Resharper 7+ now have this built in.

Benoittr
  • 4,091
  • 4
  • 27
  • 38
  • Yeah except it has to modify the project and it hardly works right...when it does work right it's really nice though. – Kelly Elton May 14 '14 at 02:20