.NET 在多个解决方案中查找死代码

发布于 2024-11-09 14:05:19 字数 219 浏览 5 评论 0原文

我们的产品包含大约 15 个解决方案,每个解决方案都有多个项目。

问题很简单:哪种工具能让我们在整个代码库中搜索死代码?

在单个解决方案中进行搜索非常容易(关于该解决方案有很多答案)。

但是,如何确定解决方案 AlphaSol 的项目 AlphaProj 中的“public void Foo()”(未在 AlphaSol 本身内使用)实际上是否在 BetaSol 等中使用?

We have a product with ~15 solutions with each a number of projects.

The question is quite simple: Which tool will enable us to search the entire codebase for dead code?

Searching within a single solution is easy enough (Lots of answers on SO for that one).

But what about determining if "public void Foo()" in project AlphaProj of solution AlphaSol ,which is not used within AlphaSol itself, is actually used in e.g. BetaSol ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

情绪失控 2024-11-16 14:05:19

尽管您有 15 个解决方案,但没有什么可以阻止您创建另一个将引用所有项目的解决方案,例如 All.sln。因此,每当您需要查找外部引用时,您都可以打开此 All.sln 解决方案并查找引用。

我们可能有大约 100 个解决方案,以及一个引用这些解决方案中所有项目的 All.sln。将一个解决方案中的所有项目添加到 All.sln 非常容易:您只需选择添加现有项目,然后选择这 15 个解决方案文件之一。您需要在添加现有项目对话框中调整文件类型才能选择解决方案文件。此外,为了使这个大型解决方案井井有条,您可以使用解决方案文件夹。

Although you have 15 solutions nothing stops you from creating another solution that will have all the projects referenced, let's say All.sln. So whenever you need to find external references you open this All.sln solution and look for the references.

We probably have about 100 solutions, and one All.sln that references all the projects from those solutions. It is easy to add all the projects from one solution to All.sln: you just select Add Existing Projects and select one of those 15 solution files. You need adjust file type in Add Existing Projects dialog to be able to select solution file. Also, to keep this big solution organized, you can use solution folders.

夜唯美灬不弃 2024-11-16 14:05:19

您可能还想查看 NDepend

由于另一个答案足够安静,足以满足您的即时需求,并且我不想听起来像一个糟糕的广告,因此我将其留给感兴趣的读者来了解有关该工具的更多信息。

You might also want to checkout NDepend.

Since the other answer is quiet sufficient for your immediate need, and I don't want to sound like a bad commercial, I leave it to the interested reader to find out more about that tool.

鹿童谣 2024-11-16 14:05:19

为了详细说明 Christian 的答案,工具 NDepend 确实可以帮助在 .NET 代码库中查找未使用的代码。 免责声明:我是该工具的开发人员之一。

NDepend 建议编写 基于 LINQ 查询的代码规则 (CQLinq)。大约提出了200条默认代码规则,其中3条专门用于未使用/死代码检测:

NDepend 集成在 Visual Studio 中,因此可以在 IDE 中直接检查/浏览/编辑这些规则< /a>.该工具还可以集成到您的 CI 流程中,并且可以构建报告,显示违反的规则和罪魁祸首代码元素。

如果您点击上面的这 3 个链接查看这些规则的源代码,您会发现涉及类型和方法的内容有点复杂。这是因为它们不仅检测未使用的类型和方法,而且还检测仅由未使用的死类型和方法(递归)使用的类型和方法。

这是静态分析,因此规则名称中的潜在前缀。如果代码元素通过反射使用,这些规则可能会将其视为未使用,但情况并非如此。

除了使用这 3 条规则之外,我还建议通过测试来衡量代码覆盖率并努力实现完全覆盖。通常,您会看到测试无法覆盖的代码实际上是可以安全丢弃的未使用/死代码。这在不清楚代码分支是否可达的复杂算法中特别有用。

To elaborate a bit on the answer of Christian, the tool NDepend can indeed help find unused code in a .NET code base. Disclaimer: I am one of the developer of this tool.

NDepend proposes to write Code Rule over LINQ Query (CQLinq). Around 200 default code rules are proposed, 3 of them being dedicated to unused/dead code detection:

NDepend is integrated in Visual Studio, 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 reports that will show rules violated and culprit code elements.

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 only by unused dead types and methods (recursive).

This is static analysis, hence the prefix Potentially in the rule names. If a code element is used only through reflection, these rules might consider it as unused which is not the case.

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/dead code 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.

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