Beispiel #1
0
        public static CodeMetric For(MethodBase method)
        {
            Throw.IfArgumentNull(method, "method");
            var methodBody = method.ToDefinition().Body;
            var methodInstructions = methodBody.Instructions;

            var ccInstructions =
                from instruction in methodInstructions
                where (
                    CcBranchOpCodes.Contains(instruction.OpCode) ||
                    instruction.OpCode == OpCodes.Switch ||
                    instruction.OpCode == OpCodes.Ret
                )
                select instruction;

            Func<Instruction, int> ccWeight = instruction =>
                instruction.OpCode == OpCodes.Switch
                ? ((Instruction[])instruction.Operand).Length
                : 1;

            var ccCatchs = methodBody.ExceptionHandlers.Count(c => c.CatchType != null);

            var value = ccInstructions.Sum(ccWeight) + ccCatchs;

            return new Cc(value);
        }