如何从 .net (C#) 项目中标记和删除不需要的代码(方法、属性)

发布于 2024-09-30 04:10:43 字数 337 浏览 3 评论 0原文

我收到了一个遗留的 .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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

吻安 2024-10-07 04:10:43

静态工具很有用 - 尝试 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.

孤独患者 2024-10-07 04:10:43

我发现 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文