public void Disassemble(MethodDef method, CilBody body, MemberMapping methodMapping) { // start writing IL code output.WriteLineComment("// Method Token is 0x{0:x8}", method.MDToken.Raw); output.WriteLineComment("// Method begins at RVA 0x{0:x}", method.RVA); output.WriteLineComment("// Code size {0} (0x{0:x})", body.GetCodeSize()); output.WriteKeyword(".maxstack "); output.WriteLiteral(body.MaxStack.ToString()); output.WriteLine(); if (method.DeclaringType.Module.Assembly != null && method.DeclaringType.Module.EntryPoint == method) { output.WriteKeyword(".entrypoint"); output.WriteLine(); } if (method.Body.HasVariables) { output.WriteKeyword(".locals "); if (method.Body.InitLocals) { output.WriteKeyword("init "); } output.WriteLine("("); output.Indent(); foreach (var v in method.Body.Variables) { output.WriteDefinition("[" + v.Index + "]", v, true); output.Write(" "); v.Type.WriteTo(output); if (!string.IsNullOrEmpty(v.Name)) { output.Write(' '); output.Write(DisassemblerHelpers.Escape(v.Name)); } if (v.Index + 1 < method.Body.Variables.Count) { output.Write(','); } output.WriteLine(); } output.Unindent(); output.WriteLine(")"); } output.WriteLine(); if (detectControlStructure && body.Instructions.Count > 0) { Instruction inst = body.Instructions[0]; var branchTargets = GetBranchTargets(body.Instructions); WriteStructureBody(method, body, new ILStructure(body), branchTargets, ref inst, methodMapping, method.Body.GetCodeSize()); } else { foreach (var inst in method.Body.Instructions) { var startLocation = output.Location; inst.WriteTo(method, body, output); if (methodMapping != null) { var next = inst.GetNext(body); // add IL code mappings - used in debugger methodMapping.MemberCodeMappings.Add( new SourceCodeMapping() { StartLocation = output.Location, EndLocation = output.Location, ILInstructionOffset = new ILRange { From = inst.Offset, To = next == null ? method.Body.GetCodeSize() : next.Offset }, MemberMapping = methodMapping }); } output.WriteLine(); } if (method.Body.HasExceptionHandlers) { output.WriteLine(); foreach (var eh in method.Body.ExceptionHandlers) { eh.WriteTo(output); output.WriteLine(); } } } }