Deriving Cyclomatic Complexity in .NET

后端 未结 4 1753
闹比i
闹比i 2021-01-13 03:09

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

4条回答
  •  灰色年华
    2021-01-13 03:47

    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);
        }
    }

提交回复
热议问题