Search code examples
c#refactoringresharperautomated-refactoring

Add custom refactoring pattern using Resharper


I have hundreds occurences of this kind of lines in my app :

public ICommand MyCommandName => ReactiveCommand.Create(TriggerCommand);

I tried without luck to add a Custom Pattern in resharper to change it to :

private ICommand _MyCommandName;
public ICommand MyCommandName => _MyCommandName ??= ReactiveCommand.Create(TriggerCommand);

Here is my pattern : enter image description here

the pattern is ambiguous and can't be saved. Here are the placeholders :

enter image description here enter image description here enter image description here enter image description here


Solution

  • I'm not using R# in Visual Studio anymore, therefore I don't have access to the Structural Search and Replace feature in VS. However, using JetBrains Rider I was able to solve your task with the following RegEx which should also work in Visual Studio 🤞🏻

    Search:

    public (\w*) (\w*) \=\> (\w*)\.(\w*)\((\w*)\)\;
    

    Replace:

    public $1 _$2;
    public $1 $2 => _$2 ??= $3.$4($5);