Skip to content
Permalink
Browse files

icsharpcode#1284: Add new option "Show IL code" to ICSharpCode.Decomp…

…iler.Console
  • Loading branch information...
kmatyaszek committed Sep 24, 2018
1 parent 6545fb3 commit f45dcf0e62cb430e7508a3001448b55a91c0caf2
Showing with 28 additions and 1 deletion.
  1. +28 −1 ICSharpCode.Decompiler.Console/Program.cs
@@ -1,11 +1,14 @@
using System;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using McMaster.Extensions.CommandLineUtils;
using ICSharpCode.Decompiler.CSharp;
using ICSharpCode.Decompiler.TypeSystem;
using ICSharpCode.Decompiler.Metadata;
using ICSharpCode.Decompiler.Disassembler;
using System.Threading;
using System.Reflection.Metadata;

namespace ICSharpCode.Decompiler.Console
{
@@ -24,6 +27,7 @@ static int Main(string[] args)
var outputOption = app.Option("-o|--outputdir <directory>", "The output directory, if omitted decompiler output is written to standard out.", CommandOptionType.SingleValue);
var typeOption = app.Option("-t|--type <type-name>", "The fully qualified name of the type to decompile.", CommandOptionType.SingleValue);
var listOption = app.Option("-l|--list <entity-type(s)>", "Lists all entities of the specified type(s). Valid types: c(lass), i(interface), s(truct), d(elegate), e(num)", CommandOptionType.MultipleValue);
var ilViewerOption = app.Option("-il|--ilcode", "Show IL code.", CommandOptionType.NoValue);
app.ExtendedHelpText = Environment.NewLine + "-o is valid with every option and required when using -p.";

app.ThrowOnUnexpectedArgument = false; // Ignore invalid arguments / options
@@ -60,6 +64,14 @@ static int Main(string[] args)
output = File.CreateText(Path.Combine(directory, outputName) + ".list.txt");
}
ListContent(inputAssemblyFileName.Value, output, kinds);
} else if (ilViewerOption.HasValue()) {
TextWriter output = System.Console.Out;
if (outputOption.HasValue()) {
string directory = outputOption.Value();
string outputName = Path.GetFileNameWithoutExtension(inputAssemblyFileName.Value);
output = File.CreateText(Path.Combine(directory, outputName) + ".ILCode.txt");
}
ShowIL(inputAssemblyFileName.Value, output);
} else {
TextWriter output = System.Console.Out;
if (outputOption.HasValue()) {
@@ -91,6 +103,21 @@ static void ListContent(string assemblyFileName, TextWriter output, ISet<TypeKin
}
}

static void ShowIL(string assemblyFileName, TextWriter output)
{
CSharpDecompiler decompiler = GetDecompiler(assemblyFileName);
ITextOutput textOutput = new PlainTextOutput();
ReflectionDisassembler disassembler = new ReflectionDisassembler(textOutput, CancellationToken.None);

disassembler.DisassembleNamespace(decompiler.TypeSystem.MainModule.RootNamespace.Name,
decompiler.TypeSystem.MainModule.PEFile,
decompiler.TypeSystem.MainModule.TypeDefinitions.Select(x => (TypeDefinitionHandle)x.MetadataToken));

output.WriteLine($"IL code: {decompiler.TypeSystem.MainModule.AssemblyName}");
output.WriteLine(textOutput.ToString());
output.Flush();
}

static void DecompileAsProject(string assemblyFileName, string outputDirectory)
{
WholeProjectDecompiler decompiler = new WholeProjectDecompiler();

0 comments on commit f45dcf0

Please sign in to comment.
You can’t perform that action at this time.