简体   繁体   中英

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.

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.

To quote from 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. To do this, first edit the run configuration to indicate the assembly that contains the code whose coverage you want to measure. 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. 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/

Providing you don't mind buying something to do it, ReSharper will go off and find unused code for you.

FxCOP is a free tool from Microsoft that includes dead code detection.

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

Couldn't hurt to download a 30 day trial to see if it does what you need anyhow.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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