/// <summary> /// Reads constructors and add them to method list for type /// </summary> /// <param name="type"></param> /// <param name="constructors"></param> private void ReadConstructors(Type type, ConstructorCollection constructors) { foreach (MethodDefinition constructor in constructors) { var method = new Method { Name = FormatMethodName(constructor), Type = type }; type.Methods.Add(method); } foreach (MethodDefinition constructor in constructors) { var method = (from m in type.Methods where m.Name == FormatMethodName(constructor) select m).SingleOrDefault(); if (constructor.Body != null) { ReadInstructions(method, constructor, constructor.Body.Instructions); } } }
/// <summary> /// Extracts methods and add them to method list for type /// </summary> /// <param name="type"></param> /// <param name="methods"></param> private void ReadMethods(Type type, MethodDefinitionCollection methods) { foreach (MethodDefinition methodDefinition in methods) { var method = new Method { Name = FormatMethodName(methodDefinition), Type = type }; 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 instrunctions /// </summary> /// <param name="method"></param> /// <param name="methodDefinition"></param> /// <param name="instructions"></param> public void ReadInstructions(Method method, MethodDefinition methodDefinition, InstructionCollection instructions) { foreach (Instruction instruction in instructions) { var instr = ReadInstruction(instruction); if (instr is MethodDefinition) { var md = instr as MethodDefinition; var type = (from n in method.Type.Namespace.Module.Namespaces from t in n.Types where t.Name == FormatTypeName(md.DeclaringType) && n.Name == t.Namespace.Name 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.Type) method.MethodUses.Add(findTargetMethod); } if (instr is FieldDefinition) { var fd = instr as FieldDefinition; var field = (from f in method.Type.Fields where f.Name == fd.Name select f).SingleOrDefault(); if (field != null) method.FieldUses.Add(field); } } }