在.NET中派生圈复杂度复杂度、NET

2023-09-03 07:35:37 作者:不吵不闹不代表我不在乎

我知道我可以通过右键单击访问圈复杂到我的code在Visual Studio 2008团队资源管理器,然后选择计算code指标。我想公开此数据为Web应用程序来显示。有谁知道任何方式通过API访问这些数据的?

I know that I can access the cyclomatic complexity to my code in Visual Studio 2008 Team Explorer by right clicking and selecting "Calculate Code Metrics". I would like to expose this data for a web application to display it. Does anybody know of any way of accessing this data through an API?

感谢您的帮助!

推荐答案

正如本 answer,人们可以利用宪兵开源工具的API来计算方法的圈复杂度

As described in this answer, one can leverage the API of the Gendarme open source tool to calculate the cyclomatic complexity of a method

ModuleDefinition module = ModuleDefinition.ReadModule(fullPathToTheAssembly);

foreach (var type in module.Types)
{
    foreach (var me in type.Methods)
    {
        if (!me.HasBody || me.IsGeneratedCode() || me.IsCompilerControlled)
            continue;
        var r = AvoidComplexMethodsRule.GetCyclomaticComplexity(me);

        Console.WriteLine("{0}: {1}", me.ToString(), r);
    }
}