English 中文(简体)
Visual Studio 2008 / C# : How to find dead code in a project?
原标题:

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

最佳回答

You can try FxCop, which is integrated in Visual Studio 2008 by the name of Code Analysis. You just have to right click the project file and Run Code Analysis .

FxCop is an application that analyzes managed code assemblies (code that targets the .NET Framework common language runtime) and reports information about the assemblies, such as possible design, localization, performance, and security improvements.

The active rules can be configured in the Code Analysis section of the project properties. For example some rules relevant to the case in hand are present in Usage Rules and Performance Rules:

  • CA1801: Review unused parameters.
  • CA1811: Avoid uncalled private code.

And for greater flexibility you also write your own custom rules (Tutorial on writing your own Code Analysis rule).

问题回答

Install JetBrains ReSharper which will highlight the unused code for you.

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.

Shift + F12 in VS is useful for a quick check :)

While I wouldn t recommend this for a large code base you can do a certain amount manually.

If you right click over a method or class and then select "Find Usages" you ll get an output of all places where it s referenced. Obviously this will be empty if it s not used.

For ongoing clean up I d install ReSharper





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签