简体   繁体   English

如何找出未调用C#.NET程序集中的代码

[英]How to find out what code in a C# .NET assembly is not being called

I have a C# .NET assembly, in visual studio 2008. If required I could upgrade it to VS 2010. 我在Visual Studio 2008中有一个C#.NET程序集。如果需要,我可以将它升级到VS 2010。

How can I find which code in the assembly is not called? 如何查找程序集中的哪些代码未被调用? Ideally if something could analyse the whole solution in one go. 理想情况下,如果有东西可以一次性分析整个解决方案。 It is too much for me to analyse it all myself manually. 我自己手动分析它对我来说太过分了。

The assembly is shared between a client and server project so I need to understand which code is used where. 程序集在客户端和服务器项目之间共享,因此我需要了解在哪里使用哪些代码。

In VS 2010 you can use code coverage together with unit tests for that - so if you can refactor your client code into actual unit tests you can use that. 在VS 2010中,您可以将代码覆盖率与单元测试结合使用 - 因此,如果您可以将客户端代码重构为实际的单元测试,则可以使用它。

To quote from MSDN : 引用MSDN

To determine what proportion of your project's code is actually being tested using unit tests, you can use the code coverage feature of Visual Studio Application Lifecycle Management. 要确定使用单元测试实际测试项目代码的比例,可以使用Visual Studio应用程序生命周期管理的代码覆盖功能。 To do this, first edit the run configuration to indicate the assembly that contains the code whose coverage you want to measure. 要执行此操作,请首先编辑运行配置以指示包含要测量其coverage的代码的程序集。 Then run tests on that code. 然后对该代码运行测试。 Detailed code coverage statistics appear in a window, and you can also see, line-by-line, which code has been tested. 详细的代码覆盖率统计信息显示在窗口中,您还可以逐行查看已测试的代码。

Try getting resharper. 尝试获得resharper。 That does that for you. 这样做对你而言。 You can get a free trial version from their website. 您可以从他们的网站获得免费试用版。 http://www.jetbrains.com/resharper/download/ http://www.jetbrains.com/resharper/download/

http://www.jetbrains.com/resharper/ http://www.jetbrains.com/resharper/

Providing you don't mind buying something to do it, ReSharper will go off and find unused code for you. 如果您不介意购买某些东西,ReSharper会为您找到未使用的代码。

FxCOP is a free tool from Microsoft that includes dead code detection. FxCOP是Microsoft的免费工具,包括死代码检测。

For best resulsts, don't mark classes/methods public unless they really need to be public. 对于最好的结果,不要将类/方法标记为公开,除非它们确实需要公开。 It assumes that any public method is used by external assemblies. 它假定外部程序集使用任何公共方法。

检查Visual Studio中的代码分析,此处提供帮助

You could try the safe delete function of ReSharper, keeping in mind it will say it's safe to delete code which is only used via reflection (such as Fluent automapping conventions). 您可以尝试使用ReSharper的安全删除功能,请记住它会说删除仅通过反射使用的代码是安全的(例如Fluent自动执行约定)。

Couldn't hurt to download a 30 day trial to see if it does what you need anyhow. 下载一个30天的试用版,看看它是否能满足您的需求,也不会受到伤害。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM