查找未使用的类在.NET项目 [英] Find unused classes in a .net project

查看:146
本文介绍了查找未使用的类在.NET项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个VS.NET 2008项目。是否有可能检查未使用anywere在项目中的类?随着FxCop的我能找到的未使用的变量和未使用code,但不使用的类。

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.

推荐答案

工具 NDepend的可以帮助查找未使用code在一个.NET code基地。 免责声明:我是这个工具的开发者之一

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

NDepend的建议写 code在LINQ查询(CQLinq)规则。各地均提出 200默认code规则,其中3人被专用于未使用/死code 的检测:

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:

  • Potentially dead Types (hence detect unused class, struct, interface, delegate...)
  • Potentially dead Methods
  • Potentially dead Fields

NDepend的集成在Visual Studio中,因此这些规则可以检查/浏览/编辑权里面的IDE 。该工具也可以被集成到CI过程,它可以建立 的该报告将显示违反规则和罪魁祸首code元素。

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.

如果您单击对这些规则的来源$ C ​​$ C这3个环节,你会看到,关于类型和方法的是一个有点复杂。这是因为它们不仅检测所用的的由未使用的死类型和方法(递归)

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).

这是的的静态分析的,因此preFIX的潜在的在规则的名称。如果code元素用于的只有的通过反射,这些规则可能会认为这是不使用的是情况并非如此。

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.

在除了使用这些3的规则,我建议测量code覆盖率的测试,争创具有全覆盖。通常情况下,你会看到,code不能覆盖测试,实际上的未使用/死的code,它可以安全地丢弃。这是在复杂的算法,其中还不清楚如果code分支可达与否尤为有用。

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.

这篇关于查找未使用的类在.NET项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆