C# 删除 Visual Studio 中未使用的代码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12790848/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Removing unused code in Visual Studio
提问by Matthew Layton
In relation to this question: "Remove unused references (!= "using")", I would like to know if there is a tool for removing unused classes, structs, delegates, etc from a Visual Studio solution.
关于这个问题:“删除未使用的引用(!=“使用”)”,我想知道是否有用于从 Visual Studio 解决方案中删除未使用的类、结构、委托等的工具。
Scenario:
设想:
I have an unorganised Visual Studio Solution which consists of 1000's of:
我有一个无组织的 Visual Studio 解决方案,其中包含 1000 个:
- Native method imports
- Structures
- Delegates
- Enumerations
- 本地方法导入
- 结构
- 代表
- 枚举
Rather than trawling through each file clicking "Find All References" and determining if the code is being used somewhere, is there any mechanism by where I can simply remove redundant code files easily?
与其通过单击“查找所有引用”并确定代码是否在某处使用来浏览每个文件,是否有任何机制可以轻松删除冗余代码文件?
Example:
例子:
//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;
}
Note to self:
注意自我:
My gut feeling is that this is going to be tricky since unlike Java, object names do not have to be identical to the file name, and multiple object declarations can reside within a single file, however in this instance (my scenario) every object is declared within its own file (with an identical name).
我的直觉是这会很棘手,因为与 Java 不同,对象名称不必与文件名相同,并且多个对象声明可以驻留在单个文件中,但是在这种情况下(我的场景)每个对象都是在它自己的文件中声明(具有相同的名称)。
采纳答案by Dmitry Khryukin
ReSharper is the best choice to clean up your code.
ReSharper 是清理代码的最佳选择。
You can use it for free thanks to ReSharper Early Access Program.
感谢ReSharper Early Access Program,您可以免费使用它。


回答by Ergwun
There are several tools that you can use to do this:
您可以使用多种工具来执行此操作:
FxCop will only find unused internal and private code. Of course if you make sure you only publicly expose code that needs to be accessible outside your assembly, then that should be good enbough.
FxCop 只会找到未使用的内部和私有代码。当然,如果您确保只公开公开需要在程序集之外访问的代码,那么这应该很好。
回答by Patrick from NDepend team
As pointed @Ergwun the tool NDependcan help to find unused methods, fields and types.
正如@Ergwun 所指出的,工具NDepend可以帮助找到未使用的方法、字段和类型。
To elaborate a bit, NDepend proposes to write Code Rule over LINQ Query (CQLinq). Around 200 default code rulesare proposed, 3 of them being dedicated to unused/dead codedetection
为了详细说明,NDepend 建议编写Code Rule over LINQ Query (CQLinq)。提出了大约200 个默认代码规则,其中 3 个专门用于未使用/死代码检测
Basically such a rule to detect unused method for example looks like:
基本上这样一个检测未使用方法的规则看起来像:
// <Name>Dead Methods</Name>
warnif count > 0
from m in Application.Methods where !m.MethodsCallingMe.Any()
select m
But this rule is naive and will return trivial false positives. There are many situations where a method is never called yet it is not unused (entry point, class constructor, finaliser...) this is why the 3 default rules are more elaborated:
但这条规则很幼稚,会返回微不足道的误报。在很多情况下,一个方法从未被调用过,但它也没有被使用(入口点、类构造函数、终结器……)这就是为什么 3 个默认规则更详细的原因:
- Potentially dead Types(hence detect unused class, struct, interface, delegate...)
- Potentially dead Methods
- Potentially dead Fields
NDepend integrates in Visual Studio 2017,2015, 2013, 2012, 2010, thus these rules can be checked/browsed/edited right inside the IDE. The tool can also be integrated into your CI process and it can build reportsthat will show rules violated and culprit code elements. NDepend has also a VS Team Services extension.
NDepend 集成在 Visual Studio 2017、2015、2013、2012、2010 中,因此可以直接在 IDE 中检查/浏览/编辑这些规则。该工具还可以集成到您的 CI 流程中,它可以构建报告,显示违反的规则和罪魁祸首的代码元素。NDepend 还有一个VS Team Services 扩展。
If you click these 3 links above toward the source code of these rules, you'll see that the ones concerning types and methods are a bit complex. This is because they detect not only unused types and methods, but also types and methods used onlyby unused dead types and methods (recursive).
如果您单击上面这 3 个指向这些规则的源代码的链接,您会发现有关类型和方法的内容有点复杂。这是因为它们不仅检测未使用的类型和方法,还检测仅由未使用的死类型和方法(递归)使用的类型和方法。
This is static analysis, hence the prefix Potentiallyin the rule names. If a code element is used onlythrough reflection, these rules might consider it as unused which is not the case.
这是静态分析,因此规则名称中的前缀Potentially。如果代码元素仅通过反射使用,则这些规则可能会将其视为未使用,但事实并非如此。
In addition to using these 3 rules, I'd advise measuring code coverage by tests and striving for having full coverage. Often, you'll see that code that cannot be covered by tests, is actually unused/deadcode that can be safely discarded. This is especially useful in complex algorithms where it is not clear if a branch of code is reachable or not.
除了使用这 3 条规则之外,我还建议通过测试来衡量代码覆盖率并努力实现完全覆盖。通常,您会看到测试无法覆盖的代码实际上是可以安全丢弃的未使用/死代码。这在不清楚代码分支是否可达的复杂算法中尤其有用。
Disclaimer: I work for NDepend.
免责声明:我为 NDepend 工作。

