private void AddTypes(IGrouping <String, Type> types, AssemblyTreeViewModel parent) { TypeReflector.RequiredBindings = SeperateAppDomainAssemblyLoader.requiredBindings; TypeReflector.ShowConstructorParameters = SeperateAppDomainAssemblyLoader.showConstructorParameters; TypeReflector.ShowFieldTypes = SeperateAppDomainAssemblyLoader.showFieldTypes; TypeReflector.ShowPropertyTypes = SeperateAppDomainAssemblyLoader.showPropertyTypes; TypeReflector.ShowInterfaces = SeperateAppDomainAssemblyLoader.showInterfaces; TypeReflector.ParseMethodBodyIL = SeperateAppDomainAssemblyLoader.parseMethodBodyIL; TypeReflector.ShowMethodArguments = SeperateAppDomainAssemblyLoader.showMethodArguments; TypeReflector.ShowMethodReturnValues = SeperateAppDomainAssemblyLoader.showMethodReturnValues; TypeReflector.ShowGetMethodForProperty = SeperateAppDomainAssemblyLoader.showGetMethodForProperty; TypeReflector.ShowSetMethodForProperty = SeperateAppDomainAssemblyLoader.showSetMethodForProperty; TypeReflector.ShowEvents = SeperateAppDomainAssemblyLoader.showEvents; TypeReflector.IncludeConstructorParametersAsAssociations = SeperateAppDomainAssemblyLoader.includeConstructorParametersAsAssociations; TypeReflector.IncludePropertyTypesAsAssociations = SeperateAppDomainAssemblyLoader.includePropertyTypesAsAssociations; TypeReflector.IncludeFieldTypesAsAssociations = SeperateAppDomainAssemblyLoader.includeFieldTypesAsAssociations; TypeReflector.IncludeMethodArgumentAsAssociations = SeperateAppDomainAssemblyLoader.includeMethodArgumentAsAssociations; //Load ILReaader Globals MethodBodyReader.LoadOpCodes(); foreach (var t in types) { TypeReflector typeReflector = new TypeReflector(t); typeReflector.ReflectOnType(); SerializableVertex vertex = new SerializableVertex( typeReflector.Name, typeReflector.ShortName, typeReflector.Constructors, typeReflector.Fields, typeReflector.Properties, typeReflector.Interfaces, typeReflector.Methods, typeReflector.Events, new List <string>(typeReflector.Associations), typeReflector.HasConstructors, typeReflector.HasFields, typeReflector.HasProperties, typeReflector.HasInterfaces, typeReflector.HasMethods, typeReflector.HasEvents); AssemblyTreeViewModel newNode = new AssemblyTreeViewModel(RepresentationType.Class, t.Name, vertex, parent); parent.Children.Add(newNode); } }
/// <summary> /// Code in this method does the following /// 1. Read the methodbodyIL string /// 2. Look at all ILInstructions and look for new objects being /// created inside the method, and add as Association /// 3. Finally return the method body IL for the diagram to use /// </summary> private String ReadMethodBodyAndAddAssociations(MethodInfo mi) { String ilBody = ""; try { if (mi == null) { return(""); } if (mi.GetMethodBody() == null) { return(""); } MethodBodyReader mr = new MethodBodyReader(mi); foreach (ILInstruction instruction in mr.Instructions) { if (instruction.Code.Name.ToLower().Equals("newobj")) { dynamic operandType = instruction.Operand; String association = operandType.DeclaringType.FullName; if (!Associations.Contains(association)) { Associations.Add(association); } } } ilBody = mr.GetBodyCode(); return(ilBody); } catch (Exception ex) { return(""); } }
/// <summary> /// Returns a friendly strign representation of this instruction /// </summary> /// <returns></returns> public string GetCode() { string result = ""; result += GetExpandedOffset(offset) + " : " + code; if (operand != null) { switch (code.OperandType) { case OperandType.InlineField: System.Reflection.FieldInfo fOperand = ((System.Reflection.FieldInfo)operand); result += " " + MethodBodyReader.ProcessSpecialTypes(fOperand.FieldType.ToString()) + " " + MethodBodyReader.ProcessSpecialTypes(fOperand.ReflectedType.ToString()) + "::" + fOperand.Name + ""; break; case OperandType.InlineMethod: try { System.Reflection.MethodInfo mOperand = (System.Reflection.MethodInfo)operand; result += " "; if (!mOperand.IsStatic) { result += "instance "; } result += MethodBodyReader.ProcessSpecialTypes(mOperand.ReturnType.ToString()) + " " + MethodBodyReader.ProcessSpecialTypes(mOperand.ReflectedType.ToString()) + "::" + mOperand.Name + "()"; } catch { try { System.Reflection.ConstructorInfo mOperand = (System.Reflection.ConstructorInfo)operand; result += " "; if (!mOperand.IsStatic) { result += "instance "; } result += "void " + MethodBodyReader.ProcessSpecialTypes(mOperand.ReflectedType.ToString()) + "::" + mOperand.Name + "()"; } catch { } } break; case OperandType.ShortInlineBrTarget: case OperandType.InlineBrTarget: result += " " + GetExpandedOffset((int)operand); break; case OperandType.InlineType: result += " " + MethodBodyReader.ProcessSpecialTypes(operand.ToString()); break; case OperandType.InlineString: if (operand.ToString() == "\r\n") { result += " \"\\r\\n\""; } else { result += " \"" + operand.ToString() + "\""; } break; case OperandType.ShortInlineVar: result += operand.ToString(); break; case OperandType.InlineI: case OperandType.InlineI8: case OperandType.InlineR: case OperandType.ShortInlineI: case OperandType.ShortInlineR: result += operand.ToString(); break; case OperandType.InlineTok: if (operand is Type) { result += ((Type)operand).FullName; } else { result += "not supported"; } break; default: result += "not supported"; break; } } return(result); }