/// <summary> /// Extracts methods and add them to method list for type. /// </summary> /// <param name="type">A type where methods will be added</param> /// <param name="methods">A collection of methods</param> private void ReadMethods(Type type, Collection<MethodDefinition> methods) { foreach (MethodDefinition methodDefinition in methods) { var method = new Method { Name = FormatMethodName(methodDefinition), DeclaringType = type, IsConstructor = methodDefinition.IsConstructor, IsPublic = methodDefinition.IsPublic, IsPrivate = methodDefinition.IsPrivate, IsProtected = !methodDefinition.IsPublic && !methodDefinition.IsPrivate, IsStatic = methodDefinition.IsStatic, IsSealed = methodDefinition.IsFinal, // not sure if final is sealed IsAbstract = methodDefinition.IsAbstract, IsSetter = methodDefinition.IsSetter, IsGetter = methodDefinition.IsGetter, IsVirtual = methodDefinition.IsVirtual, Variables = methodDefinition.Body != null ? methodDefinition.Body.Variables.Count : 0 }; var returnType = (from n in type.Namespace.Module.Namespaces from t in n.Types where t.FullName == FormatTypeName(methodDefinition.ReturnType, true) select t).SingleOrDefault(); method.ReturnType = returnType; // if null so return type is outside of assembly if (methodDefinition.ReturnType.IsGenericInstance) { method.IsReturnTypeGenericInstance = true; method.GenericReturnTypes.UnionWith(ReadGenericArguments(type.Namespace.Module, (GenericInstanceType) methodDefinition.ReturnType)); } // reading types from parameters foreach (var parameter in methodDefinition.Parameters) { var parameterType = (from n in type.Namespace.Module.Namespaces from t in n.Types where t.FullName == FormatTypeName(parameter.ParameterType, true) select t).SingleOrDefault(); if (parameterType != null) { var param = new MethodParameter { ParameterType = parameterType, IsIn = parameter.IsIn, IsOut = parameter.IsOut, IsOptional = parameter.IsOptional, }; // generic parameters if (parameter.ParameterType.IsGenericInstance) { param.IsGenericInstance = true; param.GenericTypes = ReadGenericArguments(type.Namespace.Module, (GenericInstanceType) parameter.ParameterType); } method.Parameters.Add(param); } } type.Methods.Add(method); } foreach (MethodDefinition methodDefinition in methods) { var method = (from m in type.Methods where m.Name == FormatMethodName(methodDefinition) select m).SingleOrDefault(); if (methodDefinition.Body != null) { ReadInstructions(method, methodDefinition, methodDefinition.Body.Instructions); } } }
/// <summary> /// Reads method calls by extracting instructions. /// </summary> /// <param name="method">A method where information will be added</param> /// <param name="methodDefinition">A method definition with instructions</param> /// <param name="instructions">A collection of instructions</param> public void ReadInstructions(Method method, MethodDefinition methodDefinition, Collection<Mono.Cecil.Cil.Instruction> instructions) { foreach (var instruction in instructions) { method.Instructions.Add(new Instruction { DeclaringMethod = method, // Operand = instruction.Operand.ToString() // for now operand as string should be enough }); // IL cyclomatic complexity if (instruction.OpCode.FlowControl == FlowControl.Cond_Branch) method.CyclomaticComplexity++; var operand = ReadOperand(instruction); if (operand is MethodDefinition) { var md = operand as MethodDefinition; var type = (from n in method.DeclaringType.Namespace.Module.Namespaces from t in n.Types where t.FullName == FormatTypeName(md.DeclaringType, true) select t).SingleOrDefault(); method.TypeUses.Add(type); var findTargetMethod = (from m in type.Methods where m.Name == FormatMethodName(md) select m).SingleOrDefault(); if (findTargetMethod != null && type == method.DeclaringType) method.MethodUses.Add(findTargetMethod); } if (operand is FieldDefinition) { var fd = operand as FieldDefinition; var field = (from f in method.DeclaringType.Fields where f.Name == fd.Name select f).SingleOrDefault(); if (field != null) method.FieldUses.Add(field); } } }
public static BitmapSource GetIcon(Method method) { if (method.IsPublic) return Method; if (method.IsPrivate) return PrivateMethod; if (method.IsProtected) return ProtectedMethod; if (method.IsGetter || method.IsSetter) { if (method.IsPublic) return PropertyMethod; if (method.IsPrivate) return PrivatePropertyMethod; if (method.IsProtected) return ProtectedPropertyMethod; } return Method; }