How to convert C # object initializer to use constructor using structural find and replace Resharper

I want to use Resharper structural search and replace the template to automatically replace examples of this:

new Fruit { Name = "Apple", IsTasty = true }

Wherein:

new Fruit("Apple", true)

(Note that the required constructor already exists)

I tried various combinations:

new $type$ { Name = $name$, IsTasty = $isTasty$ };

... using various types of Placeholder, but R # does not find any of the examples in my code. Has anyone done this before?

+4
source share
2 answers

So, I just did it in resharper (9.0.2, I think)

Create Template: FruityChange

  • new $ type $ {Name = $ param1 $, IsTasty = $ param2 $}
  • $ type $ - type
  • $ param1 $ - type string expression
  • $param2 $- bool
  • " "

Fruit

public class Fruit
{
    public Fruit(string name, bool isTasty)
    {
        Name = name;
        IsTasty = isTasty;
    }

    public string Name { get; set; }
    public bool IsTasty { get; set; }
}

, alt-tab " ". , , : -)

+4

, R # , .

"" "".

enter image description here

+2

All Articles