计算 .NET Core 项目的代码指标?

Calculate Code Metrics for .NET Core Projects?

我正在研究 ASP.NET Core 和 .NET Core 项目。对于经典 C# 项目 Visual Studio 2015 具有计算代码指标的功能。 Preview 2 Tooling 中缺少对 .NET Core 的支持。

在工具更完整之前,有人知道解决方法吗?

NDepend 支持.NET 核心as per this

此外,SonarQube 可以提供您正在寻找的一些指标(例如代码行数、圈复杂度)。根据 release notes,版本 2.3+ 支持 .NET Core

现在可以在 VS 2019 Preview 1 中使用,据此:

https://developercommunity.visualstudio.com/idea/351625/netcore-code-metrics.html?childToView=404758

我将 Microsoft.CodeAnalysis.FxCopAnalyzers nuget 包添加到我的 .Net Core 项目中。这让我可以右键单击和 运行 代码分析,效果很好。

下一个挑战是让 计算代码指标 菜单选项起作用。

不幸的是,这仍然会产生错误:

An error occurred while calculating code metrics for target file '.dll' in project . Could not identify platform

坏消息是,这不会在 Visual Studio 2017 年修复,但它在 Visual Studio 2019 年(当前预览版 2)中确实按预期工作。

好消息是我成功地使用 Roslyn Analyzers github project. I cloned the repo (https://github.com/dotnet/roslyn-analyzers.git) 计算了指标。然后构建解决方案。创建的工件之一是路径 <your dev root>\roslyn-analyzers\artifacts\bin\Metrics\Debug\net46\Metrics.exe

中的 Metrics.exe

打开命令提示符并运行宁Metrics.exe产生有用的帮助文本。记得指定项目或sln文件和/out:<metrics fine name>.xml

Usage: Metrics.exe <arguments>

Help for command-line arguments:

/project:<project-file>  [Short form: /p:<project-file>]
Project(s) to analyze.

/solution:<solution-file>  [Short form: /s:<solution-file>]
Solution(s) to analyze.

/out:<file>  [Short form: /o:<file>]
Metrics results XML output file.

/quiet  [Short form: /q]
Silence all console output other than error reporting.

/help  [Short form: /?]
Display this help message.

这会生成一个相当笨重的 XML 文件,然后可以对其进行解析以提取您需要的指标。这有点痛苦,但对我有用,因为我只想对相当广泛的 .Net Core 2 代码库进行一次性分析。