c# - Resharper 重构模式将局部变量赋值包装在 try catch 中

标签 c# try-catch resharper

如果我有以下代码

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/

相关文章:

Resharper 2016.1.2 NUnit.Core.UnsupportedFrameworkException 与 NUnit 3 测试

javascript - 方法如何将 null 作为接口(interface)返回

c# - 如何将字符串转换为int C#

c# - 将列表转换为字符串列表

Java Try-Catch - 它是如何执行的?

javascript - 如何在没有错误的情况下访问catch javascript

c# - 在 Internet 中断时继续尝试与服务器通信

c# - wcf服务中http绑定(bind)和tcp绑定(bind)的区别

c# - 如何从内部的 try/catch 外部打破循环?

unit-testing - Visual Studio 中的测试 session 窗口 (Resharper) 中的 "Success: Test not run"