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

前端 未结 5 1765
广开言路
广开言路 2020-12-14 19:28

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

5条回答
  •  北荒
    北荒 (楼主)
    2020-12-14 19:48

    A very useful tool for this is the NDepend dependency 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.

    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 :

    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.

提交回复
热议问题