如何从 .net (C#) 项目中标记和删除不需要的代码(方法、属性)
我收到了一个遗留的 .Net C# 解决方案,其中包含许多类库项目可供审查、重构和重用。该解决方案没有在任何地方使用,并且位于代码垃圾场中。不过,该解决方案可以正确编译。
主类库需要 4 个主要方法。我只想保留解决方案中所有其他项目中这 4 个方法使用的所有后续类、方法和属性,并删除对我来说是垃圾的所有其他代码。目前,我正在从 Visual Studio 2010 的“调用层次结构”功能中的这 4 个主要方法进行手动跟踪。
是否有一个自动化过程可以快速识别与我的主要方法相关的代码并将其提取到全新的闪亮解决方案(希望能够成功构建),以便我只需查看相关代码,而无需查看我的四个不需要的其他代码主要方法。
谢谢。
I have received a legacy .Net c# solution with many class library projects to review, re-factor and reuse. This solution is not used anywhere and lying in the code junkyard. The solution is compiling properly though.
There are 4 primary methods, that is needed from the main class library. I just want to retain all the subsequent classes, methods, properties used by these 4 methods from all the other projects in the solution and strip off all the other codes which is junk for me. Currently, i am going by manual tracing from these 4 main methods in Visual Studio 2010's "Call Hierarchy" feature.
Is there a automated process to quickly identify the related codes to my main methods and extract it to brand new shiny solution (which hopefully builds successfully) so that i just have to see the relevant code devoid of other codes that is not needed by my four main methods.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
静态工具很有用 - 尝试 NDepend - 但动态调用意味着代码库的任何部分都可以通过这些方法访问。尝试运行代码覆盖率工具,例如 NCover 以及一套广泛的单元测试,也许还可以进行手动测试,然后分析工具的输出。
Static tools are useful -- try NDepend -- but dynamic calls mean that any part of your codebase might be accessed by these methods. Try running a code coverage tool such as NCover with an extensive suite of unit tests, and perhaps manual tests as well, and then analyze the tool's output.
我发现 ReSharper 对于检测无法访问或未使用的代码非常有用,所以我当然会尝试一下。您可以从 http://www.jetbrains.com/resharper/ 下载演示版本。
它可能不会为您做所有事情,并且在多个类中查找未使用的代码(换句话说,未调用的公共方法)并不那么容易,但这是一个好的开始。
与此相关的是,我还建议您在进行任何重构之前进行一组良好的单元测试,以便您可以轻松地发现是否/何时破坏功能。
I find ReSharper pretty useful for detecting unreachable or unused code, so I'd certainly give that a go. You can download a demo version from http://www.jetbrains.com/resharper/.
It may not do everything for you, and finding unused code across multiple classes (in other words public methods that are not called) isn't so easy, but it's a good start.
Related to this, I would also advise you get a good set of unit tests in place before you undertake any refactoring, so that you can easily spot if/when you break functionality.