using System.Reflection.Emit; using System.Reflection.Metadata; public class X86InstructionVisitor : IX86Visitor { public void Visit(MovInstruction instruction) { Console.WriteLine("MOV {0}, {1}", instruction.Operand1, instruction.Operand2); } public void Visit(AddInstruction instruction) { Console.WriteLine("ADD {0}, {1}", instruction.Operand1, instruction.Operand2); } } // Create a dummy stream of x86 instructions byte[] instructions = new byte[] { 0x8b, 0x45, 0x08, 0x8d, 0x55, 0xfc }; // Create a new X86InstructionVisitor var visitor = new X86InstructionVisitor(); // Parse the instructions X86Parser.Parse(instructions, visitor);
using System.Reflection; using System.Reflection.Emit; using System.Reflection.Metadata; public class X86Disassembler : IX86Visitor { private readonly TextWriter output; public X86Disassembler(TextWriter output) { this.output = output; } public void Visit(JumpInstruction instruction) { output.WriteLine("JMP {0}", instruction.TargetOffset); } public void Visit(CallInstruction instruction) { output.WriteLine("CALL {0} ({1})", instruction.TargetOffset, instruction.Operand1); } } // Load the .NET assembly var assembly = Assembly.LoadFrom("MyAssembly.dll"); // Get a reference to the method to disassemble var method = assembly.GetType("MyClass").GetMethod("MyMethod"); // Get the method's IL stream var ilStream = method.GetMethodBody().GetILAsByteArray(); // Create a new X86Disassembler var disassembler = new X86Disassembler(Console.Out); // Parse the x86 instructions in the method X86Parser.Parse(ilStream, disassembler);Package library: This interface is part of the .NET Framework and is located in the `System.Reflection.Emit` namespace.