void DoDisassemble(IDnlibDef item, ITextOutput output, ReflectionDisassembler disassembler) { if (item is ModuleDef) { var module = (ModuleDef)item; disassembler.WriteAssemblyReferences(module); if (module.Assembly != null) disassembler.WriteAssemblyHeader(module.Assembly); output.WriteLine(); disassembler.WriteModuleHeader(module); } else if (item is TypeDef) { disassembler.DisassembleType((TypeDef)item); } else if (item is MethodDef) { disassembler.DisassembleMethod((MethodDef)item); } else if (item is FieldDef) { disassembler.DisassembleField((FieldDef)item); } else if (item is PropertyDef) { disassembler.DisassembleProperty((PropertyDef)item); } else if (item is EventDef) { disassembler.DisassembleEvent((EventDef)item); } }
public override void DecompileProperty(PropertyDefinition property, ITextOutput output, DecompilationOptions options) { ReflectionDisassembler rd = new ReflectionDisassembler(output, detectControlStructure, options.CancellationToken); rd.DisassembleProperty(property); if (property.GetMethod != null) { output.WriteLine(); rd.DisassembleMethod(property.GetMethod); } if (property.SetMethod != null) { output.WriteLine(); rd.DisassembleMethod(property.SetMethod); } foreach (var m in property.OtherMethods) { output.WriteLine(); rd.DisassembleMethod(m); } }
public override void DecompileProperty(PropertyDefinition property, ITextOutput output, DecompilationOptions options) { ReflectionDisassembler rd = CreateDisassembler(output, options); rd.DisassembleProperty(property); if (property.GetMethod != null) { output.WriteLine(); rd.DisassembleMethod(property.GetMethod); } if (property.SetMethod != null) { output.WriteLine(); rd.DisassembleMethod(property.SetMethod); } foreach (var m in property.OtherMethods) { output.WriteLine(); rd.DisassembleMethod(m); } }
public override void Decompile(EventDef ev, IDecompilerOutput output, DecompilationContext ctx) { ReflectionDisassembler rd = CreateReflectionDisassembler(output, ctx, ev); rd.DisassembleEvent(ev, addLineSep: true); if (ev.AddMethod != null) { output.WriteLine(); rd.DisassembleMethod(ev.AddMethod, true); } if (ev.RemoveMethod != null) { output.WriteLine(); rd.DisassembleMethod(ev.RemoveMethod, true); } foreach (var m in ev.OtherMethods) { output.WriteLine(); rd.DisassembleMethod(m, true); } }
public override void Decompile(PropertyDef property, IDecompilerOutput output, DecompilationContext ctx) { ReflectionDisassembler rd = CreateReflectionDisassembler(output, ctx, property); rd.DisassembleProperty(property, addLineSep: true); if (property.GetMethod != null) { output.WriteLine(); rd.DisassembleMethod(property.GetMethod, true); } if (property.SetMethod != null) { output.WriteLine(); rd.DisassembleMethod(property.SetMethod, true); } foreach (var m in property.OtherMethods) { output.WriteLine(); rd.DisassembleMethod(m, true); } }
public override void DecompileEvent(EventDefinition ev, ITextOutput output, DecompilationOptions options) { ReflectionDisassembler rd = new ReflectionDisassembler(output, detectControlStructure, options.CancellationToken); rd.DisassembleEvent(ev); if (ev.AddMethod != null) { output.WriteLine(); rd.DisassembleMethod(ev.AddMethod); } if (ev.RemoveMethod != null) { output.WriteLine(); rd.DisassembleMethod(ev.RemoveMethod); } foreach (var m in ev.OtherMethods) { output.WriteLine(); rd.DisassembleMethod(m); } }
public override void DecompileProperty(PropertyDefinition property, ITextOutput output, DecompilationOptions options) { ReflectionDisassembler rd = new ReflectionDisassembler(output, detectControlStructure, options.CancellationToken); rd.DisassembleProperty(property); if (property.GetMethod != null) { output.WriteLine(); rd.DisassembleMethod(property.GetMethod); } if (property.SetMethod != null) { output.WriteLine(); rd.DisassembleMethod(property.SetMethod); } foreach (var m in property.OtherMethods) { output.WriteLine(); rd.DisassembleMethod(m); } }
public override void DecompileEvent(EventDefinition ev, ITextOutput output, DecompilationOptions options) { ReflectionDisassembler rd = CreateDisassembler(output, options); rd.DisassembleEvent(ev); if (ev.AddMethod != null) { output.WriteLine(); rd.DisassembleMethod(ev.AddMethod); } if (ev.RemoveMethod != null) { output.WriteLine(); rd.DisassembleMethod(ev.RemoveMethod); } foreach (var m in ev.OtherMethods) { output.WriteLine(); rd.DisassembleMethod(m); } }
public override void DecompileMethod(MethodDefinition method, ITextOutput output, DecompilationOptions options) { var dis = new ReflectionDisassembler(output, detectControlStructure, options.CancellationToken); dis.DisassembleMethod(method); }
public override void DecompileEvent(EventDefinition ev, ITextOutput output, DecompilationOptions options) { ReflectionDisassembler rd = new ReflectionDisassembler(output, detectControlStructure, options.CancellationToken); rd.DisassembleEvent(ev); if (ev.AddMethod != null) { output.WriteLine(); rd.DisassembleMethod(ev.AddMethod); } if (ev.RemoveMethod != null) { output.WriteLine(); rd.DisassembleMethod(ev.RemoveMethod); } foreach (var m in ev.OtherMethods) { output.WriteLine(); rd.DisassembleMethod(m); } }
public override void DecompileMethod(MethodDefinition method, ITextOutput output, DecompilationOptions options) { var dis = new ReflectionDisassembler(output, detectControlStructure, options.CancellationToken); dis.DisassembleMethod(method); }
public string GetIl(string code) { code = code.Trim().EndsWith(";") ? code : $"return {code};"; var codeLines = code.Split("\\r\\n", StringSplitOptions.RemoveEmptyEntries); var retval = codeLines.Last().Contains("return") ? "object" : "void"; string toExecute = $@" namespace Eval {{ public class Code {{ public {retval} Main() {{ {code} }} }} }} "; var opts = CSharpParseOptions.Default.WithLanguageVersion(LanguageVersion.Latest).WithKind(SourceCodeKind.Regular); var scriptSyntaxTree = CSharpSyntaxTree.ParseText(toExecute, opts); var compOpts = new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary).WithOptimizationLevel(OptimizationLevel.Release).WithAllowUnsafe(true).WithPlatform(Platform.AnyCpu); var compilation = CSharpCompilation.Create(Guid.NewGuid().ToString(), options: compOpts, references: References).AddSyntaxTrees(scriptSyntaxTree); var sb = new StringBuilder(); using (var pdb = new MemoryStream()) using (var dll = new MemoryStream()) { var result = compilation.Emit(dll, pdb); if (!result.Success) { sb.AppendLine("Emit Failed"); sb.AppendLine(string.Join(Environment.NewLine, result.Diagnostics.Select(a => a.GetMessage()))); } else { dll.Seek(0, SeekOrigin.Begin); using (var module = ModuleDefinition.ReadModule(dll)) using (var writer = new StringWriter(sb)) { module.Name = compilation.AssemblyName; var plainOutput = new PlainTextOutput(writer); var rd = new ReflectionDisassembler(plainOutput, CancellationToken.None) { DetectControlStructure = false }; var ignoredMethods = new[] { ".ctor" }; var methods = module.Types.SelectMany(a => a.Methods).Where(a => !ignoredMethods.Contains(a.Name)); foreach (var method in methods) { rd.DisassembleMethod(method); plainOutput.WriteLine(); } } } var final = sb.ToString(); return(final); } }
public static void GetMethodDecompiledILCode(ITextOutput textOutput, MethodDefinition method) { ReflectionDisassembler reflectionDisassembler = new ReflectionDisassembler(textOutput, false, CancellationToken.None); reflectionDisassembler.DisassembleMethod(method); }