删除Visual Studio中未使用的代码

ser*_*0ne 30 .net c# code-cleanup visual-studio-2010 visual-studio

关于这个问题:" 删除未使用的引用(!="using") ",我想知道是否有一个工具可以从Visual Studio解决方案中删除未使用的类,结构,委托等.

场景:

我有一个无组织的Visual Studio解决方案,包含1000个:

  • 本机方法导入
  • 结构
  • 代表
  • 枚举

而不是拖动每个文件点击"查找所有引用"并确定代码是否在某处使用,是否有任何机制可以轻松删除冗余代码文件?

例:

//This class contains a method called getRandomValue which returns type RANDOM
public class NativeMethods
{
    [DllImport("random.dll")]
    public static extern RANDOM getRandomValue();
}

//This is the RANDOM object as referenced by getRandomValue();
[StructLayout(LayoutKind.Sequential)]
public struct RANDOM
{
    uint a;
    uint b;
    uint c;
}

//This is redundant since nothing is referencing it.
[StructLayout(LayoutKind.Sequential)]
public struct MESSAGE
{
    IntPtr sender;
    IntPtr recipient;
    char[] mText;
}
Run Code Online (Sandbox Code Playgroud)

注意自我:

我的直觉是,这将是棘手的,因为与Java不同,对象名称不必与文件名相同,并且多个对象声明可以驻留在单个文件中,但是在这个实例中(我的场景)每个对象都是在其自己的文件中声明(具有相同的名称).

Dmi*_*kin 17

ReSharper是清理代码的最佳选择.

ReSharper Early Access Program可以免费使用它.

在此输入图像描述


Erg*_*wun 12

您可以使用几种工具来执行此操作:

FxCop只会找到未使用的内部和私有代码.当然,如果你确定只公开暴露需要在程序集外部访问的代码,那么这应该是很好的.