visual-studio Visual Studio 2008 / C#:如何在项目中查找死代码?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2020297/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 11:22:04  来源:igfitidea点击:

Visual Studio 2008 / C# : How to find dead code in a project?

c#visual-studiomaintenance

提问by TalkingCode

How do I find dead code in a Visual Studio 2008 C# project? Like unused classes, unused variables or unused resources?

如何在 Visual Studio 2008 C# 项目中找到死代码?像未使用的类、未使用的变量或未使用的资源?

回答by Jo?o Angelo

You can try FxCop, which is integrated in Visual Studio 2008 by the name of Code Analysis. You just have to right click the project file and 'Run Code Analysis'.

您可以尝试FxCop,它以代码分析的名称集成在 Visual Studio 2008 中。您只需右键单击项目文件并“运行代码分析”。

FxCop is an application that analyzes managed code assemblies (code that targets the .NET Framework common language runtime) and reports information about the assemblies, such as possible design, localization, performance, and security improvements.

FxCop 是一个应用程序,它分析托管代码程序集(面向 .NET Framework 公共语言运行时的代码)并报告有关程序集的信息,例如可能的设计、本地化、性能和安全改进。

The active rules can be configured in the Code Analysis section of the project properties. For example some rules relevant to the case in hand are present in Usage Rules and Performance Rules:

可以在项目属性的代码分析部分配置活动规则。例如,一些与手头案例相关的规则存在于使用规则和性能规则中:

  • CA1801: Review unused parameters.
  • CA1811: Avoid uncalled private code.
  • CA1801:查看未使用的参数。
  • CA1811:避免未调用的私有代码。

And for greater flexibility you also write your own custom rules (Tutorial on writing your own Code Analysis rule).

为了获得更大的灵活性,您还可以编写自己的自定义规则(编写自己的代码分析规则的教程)。

回答by David M

Install JetBrains ReSharperwhich will highlight the unused code for you.

安装JetBrains ReSharper,它将为您突出显示未使用的代码。

回答by Rob Levine

A very useful tool for this is the NDependdependency analysis tool. You can load your Visual Studio project into this, and it will analyse the dependencies of all your assemblies, types and methods. It gives you a wealth of information on the dependenices, including (but in no way limited to!) methods/types that are not used by anything at all.

一个非常有用的工具是NDepend依赖分析工具。您可以将 Visual Studio 项目加载到其中,它将分析所有程序集、类型和方法的依赖关系。它为您提供了有关依赖项的大量信息,包括(但绝不限于!)根本不使用的方法/类型。

You can view the dependencies either graphically, or in a list, and can write your own custom dependency queries such as this - a simple CQL query show potentially unused methods :

您可以以图形方式或列表方式查看依赖项,并且可以编写自己的自定义依赖项查询,例如 - 一个简单的 CQL 查询显示可能未使用的方法:

SELECT 
  METHODS         // Get me a list of methods
WHERE 
  MethodCa == 0   // Where their afferent coupling is zero, (afferent coupling being the number of other methods that call it)

A highly recommended tool.

强烈推荐的工具。

回答by leppie

Shift + F12 in VS is useful for a quick check :)

VS 中的 Shift + F12 可用于快速检查:)

回答by ChrisF

While I wouldn't recommend this for a large code base you can do a certain amount manually.

虽然我不建议将这个用于大型代码库,但您可以手动执行一定数量的操作。

If you right click over a method or class and then select "Find Usages" you'll get an output of all places where it's referenced. Obviously this will be empty if it's not used.

如果您右键单击一个方法或类,然后选择“查找用法”,您将获得引用它的所有位置的输出。显然,如果不使用它,它将是空的。

For ongoing clean up I'd install ReSharper

为了持续清理,我会安装ReSharper