检测 C# 中的死代码

发布于 2024-10-06 02:22:12 字数 25 浏览 13 评论 0原文

如何检测 C# 应用程序中的死代码?

How can I detect dead code in my C# application?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

生生漫 2024-10-13 02:22:12

ReSharper 可以处理这个问题。您还可以查看 NDepend

如果您不想为其中任何一个付费,我相信您可以使用 FxCop 分析您的项目,它也会识别死代码。

ReSharper can handle that. You could also check out NDepend.

If you don't feel like paying for either of those, I believe you can analyze your project with FxCop and it will also identify dead code.

空袭的梦i 2024-10-13 02:22:12

编译您的代码并检查错误列表中的警告。以下代码:

    public ActionResult Index() {
        ViewData["Message"] = "Welcome to ASP.NET MVC!";
        return View();
        return null;  // unreachable
    }

产生此警告:

Warning 11  Unreachable code detected   <fullpath>\HomeController.cs    13  13  <prjname>

JetBrains ReSharper (http://jetbrains.com/resharper)* 等工具也可以动态执行此分析并突出显示死代码。

* ReSharper 是一个商业工具。

Compile your code and check the warnings in the Error List. The following code:

    public ActionResult Index() {
        ViewData["Message"] = "Welcome to ASP.NET MVC!";
        return View();
        return null;  // unreachable
    }

produces this warning:

Warning 11  Unreachable code detected   <fullpath>\HomeController.cs    13  13  <prjname>

Tools like JetBrains ReSharper (http://jetbrains.com/resharper)* can also perform this analysis on the fly and highlight dead code.

* ReSharper is a commercial tool.

吃兔兔 2024-10-13 02:22:12

Resharper 识别死代码和未使用的参数/局部变量,FxCop

Resharper identifies dead code and unused parameters/locals and so does FxCop.

揽清风入怀 2024-10-13 02:22:12

请注意,这些工具不会检测注释中的死代码。
例如,以下内容:

// var a = 123;
// DoSomething(a);

不会被检测为死代码。

截至 2020 年 7 月,我找不到任何可以检测注释中死代码的代码检查工具。因此我自己开发了一个(基于Roslyn)并在MIT许可下发布:
https://github.com/mristin/dead-csharp

Mind that these tools do not detect the dead code in the comments.
For example, the following:

// var a = 123;
// DoSomething(a);

will not be detected as dead code.

As of July 2020, I could not find any code inspection tool that could detect dead code in the comments. Therefore I developed one on my own (based on Roslyn) and published it under MIT license:
https://github.com/mristin/dead-csharp.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文