使用 R# Structural Search and Replace 迁移代码

Migrate code with R# Structural Search and Replace

我有想要迁移到新数据模型的遗留系统。它归结为许多类型名称更改。为了不破坏现有代码,我打算使用编译器开关:

#if NEW
    var variable = new MyNewType();
#else
    MyOldType variable = new MyOldType();
#endif

我创建了一个 Resharper 替换模式:

#if NEW
    var $variable$ = new $type$();
#else
    $type$ $variable$ = new $type$();
#endif

type 定义为类型占位符,variable 是标识符占位符。但是,在解析该表达式时出现语法错误:

unexpected placeholder "variable"

无法使用 Resharper 引入编译器开关,还是我哪里出错了?

据我所知,R# 不支持这种情况。我相信这是因为 R# 依赖于 current 代码的抽象语法树 (AST)。在您的情况下,AST 取决于根据是否定义 NEW 实际编译了哪些语句。