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

回眸只為那壹抹淺笑 提交于 2019-12-04 07:53:56

So I just did this in resharper (9.0.2 I think)

Create pattern: FruityChange

  • new $type$ { Name = $param1$, IsTasty = $param2$ }
  • $type$ is a type
  • $param1$ is an expression of type string
  • $param2$ is an expression of type bool
  • Pattern Severity "Show as error"

with class Fruit

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

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

And that underlined the expression in the code editor and alt-tab gave me the "Replace With" option for it. It at least works on my machine :-)

By default, it seems R# treats your placeholders as identifiers, and the pattern match fails.

Double-click each placeholder on the right side, and change "identifier" to "expression".

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!