0

I'm creating a program that converts IL byte code to another language. But I'm stuck on the first part: getting that IL byte code.

I want, directly from my program in C#, to be able for the user to input a .NET app, and get its IL byte code.

I've tried to use ICSharpCode.Decompiler, however I cannot find a way to decompile to IL byte code instead of the C# code.

For example, my code currently decompiles the file as C# code:

using ICSharpCode.Decompiler;
using ICSharpCode.Decompiler.CSharp;

Console.Write("Input file: ");
var path = Console.ReadLine()?.Replace("\"", string.Empty);

var decompiler = new CSharpDecompiler(path, new DecompilerSettings());
var code = decompiler.DecompileWholeModuleAsString();

File.WriteAllText("code.txt", code);

But this code, as said before, decompiles to C# code. However I would want it to decompile to IL byte code. Is there any way to do this?

ShiningLea
  • 132
  • 1
  • 12
  • 1
    the file is the il byte code. Its a stream of bytes. Are you wanting to get the human readable il code – pm100 Apr 07 '22 at 18:50
  • Does the [IL Disassembler](https://learn.microsoft.com/en-us/dotnet/framework/tools/ildasm-exe-il-disassembler) meet your needs? Other ideas in [Jon Skeet's answer](https://stackoverflow.com/a/6574875/14956277) to [Convert C# code to IL code](https://stackoverflow.com/questions/6574858/convert-c-sharp-code-to-il-code). – D M Apr 07 '22 at 18:51
  • @pm100 Not necessarily, since I'll convert it later. – ShiningLea Apr 07 '22 at 18:54
  • @DM Ildasm looks good, but it sounds like a pain to call externally (in my app at least). – ShiningLea Apr 07 '22 at 18:55
  • 1
    Why is it a pain? Store a file, call lldasm, read the output. Easy. – akop Apr 07 '22 at 19:05
  • Ah, lldasm can't handle plain code. My bad. – akop Apr 07 '22 at 19:07
  • I am sure ILSpy can do that. I am using the command line version (https://github.com/icsharpcode/ILSpy/tree/master/ICSharpCode.ILSpyCmd). In the worst case (if the types you need to do that are not public) you'll need to clone their repository and change the type visibility. Depending on your needs you may also leverage Mono.Cecil – Vagaus Apr 07 '22 at 19:26

1 Answers1

0

I beleive this project actually displays the IL code and it's written in C#.

https://github.com/icsharpcode/ILSpy