如果我有以下代码
var x = foo();
DoSomethingWith(x);
哪里
Foo foo(){...}
有时我想将其包装在异常处理程序中,如下所示
Foo x;
try{
x = foo();
}catch(Exception e){
$InsertPoint$
}
DoSomethingWith(x);
现在我知道如何处理模板,并且我已经看到了一些有关如何进行结构模式匹配的信息。在 R# 中是否可以构造一个模式来获取 x 的类型(它被声明为 var ),然后使用显式声明生成包装形式。
最佳答案
尝试匹配 $type$ $id$ = $expr$;
并替换为
$type$ $id$;
try{
$id$ = $expr$;
}catch(Exception e){
//Insert code here
}
使用结构化搜索和替换。将 $expr$
标记为 表达式
而不是 标识符
关于c# - Resharper 重构模式将局部变量赋值包装在 try catch 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21432719/