在一个.net项目中寻找未使用的类[英] Find unused classes in a .net project

本文是小编为大家收集整理的关于在一个.net项目中寻找未使用的类的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我有一个vs.net 2008项目.是否可以检查项目中未使用的类吗?使用FXCOP,我可以找到未使用的变量和未使用的代码,但没有未使用的类.

推荐答案

工具 ndeppert 可以帮助您在.NET代码库中找到未使用的代码. 免责声明:我是此工具的开发人员之一.

为了详细说明,ndependess提议写 linq Query(cqlinq).围绕提出了200个默认代码规则,其中3个是专门用于未使用的/死亡代码检测

基本上是检测未使用方法的规则,例如:

// <Name>Dead Methods</Name>
warnif count > 0 
from m in Application.Methods where !m.MethodsCallingMe.Any()
select m

 nDepent的规则,要找到未使用的方法(死方法)

但是,该规则是幼稚的,将返回琐碎的误报.在许多情况下,从未调用过一种方法,但它尚未未使用(切入点,类构造函数,最终确定器...)这就是为什么3个默认规则更详细的原因:

ndependepent集成在Visual Studio中,因此这些规则可以 checked/aksed/browsed/浏览/编辑.该工具也可以集成到您的CI过程中,并且可以构建报告这将显示出违反规则和罪魁祸首元素.

如果您单击了这些规则的源代码的这3个链接,您会发现有关类型和方法的链接有点复杂.这是因为他们不仅检测未使用的类型和方法,还检测到使用的类型和方法仅通过未使用的死类型和方法(递归).

.

这是静态分析,因此在规则名称中的前缀可能是.如果仅通过反射使用代码元素 ,则这些规则可能将其视为未使用的情况,而不是这种情况.

除了使用这3个规则外,我建议通过测试测量代码覆盖范围并努力进行全面覆盖.通常,您会看到无法被测试所涵盖的代码实际上是可以安全丢弃的未使用/死亡/死亡的代码.这在复杂算法中特别有用,在复杂的算法中,尚不清楚代码是否可以到达.

其他推荐答案

resharper "> isharper 在您的项目和解决方案中

本文地址:https://www.itbaoku.cn/post/15051.html

问题描述

I have a VS.NET 2008 project. Is it possible to check for classes that are not used anywere in the project? With FXcop I can find unused variables and unused code, but not unused classes.

推荐答案

The tool NDepend can help find unused code in a .NET code base. Disclaimer: I am one of the developer of this tool.

To elaborate a bit, NDepend proposes to write Code Rule over LINQ Query (CQLinq). Around 200 default code rules are proposed, 3 of them being dedicated to unused/dead code detection

Basically such a rule to detect unused method for example looks like:

// <Name>Dead Methods</Name>
warnif count > 0 
from m in Application.Methods where !m.MethodsCallingMe.Any()
select m

NDepend rule to find unused methods (dead methods)

But this rule is naive and will return trivial false positives. There are many situations where a method is never called yet it is not unused (entry point, class constructor, finaliser...) this is why the 3 default rules are more elaborated:

NDepend is integrated in Visual Studio, hence these rules can be checked/browsed/edited right inside the IDE. The tool can also be integrated into your CI process and it can build reports that will show rules violated and culprit code elements.

If you click these 3 links toward the source code of these rules, you'll see that the ones concerning types and methods are a bit complex. This is because they detect not only unused types and methods, but also types and methods used only by unused dead types and methods (recursive).

This is static analysis, hence the prefix Potentially in the rule names. If a code element is used only through reflection, these rules might consider it as unused which is not the case.

In addition to using these 3 rules, I'd advise measuring code coverage by tests and striving for having full coverage. Often, you'll see that code that cannot be covered by tests, is actually unused/dead code that can be safely discarded. This is especially useful in complex algorithms where it is not clear if a branch of code is reachable or not.

其他推荐答案

Resharper (with solution-wide checking on) automatically notified you of unused classes in your project & solution