0

ILSpy is using cecil along with it's own libraries to accomplish this, i have heard that .NETReflector is using his own private algorithm to generate the higher level c#,f#,vb code from MSIL, any methods/references on how to do this without dependencies ?

EDIT: i'm now satified with ILSpy and cecil as a backbone

SOLUTION:

string GetSourceCode(string path) {
        var asmDef = AssemblyDefinition.ReadAssembly(path);
        var strBuilder = new StringBuilder();
        foreach (var type in asmDef.MainModule.Types) {
            if (!type.IsCompilerGenerated()) {
                AstBuilder builder = new AstBuilder(new DecompilerContext(asmDef.MainModule));
                builder.AddType(type);
                var output = new StringWriter();
                builder.GenerateCode(new PlainTextOutput(output));
                strBuilder.Append(output.ToString());
                output.Dispose();
            }
        }
        return strBuilder.ToString();
    }
0x3h
  • 452
  • 9
  • 22

1 Answers1

1

Well, first thing you have to understand, is that there is no such thing as plain source code in compiled dll or exe file. Upon compilation C# code is compiled into CIL. Mono.Cecil reads dll or exe file as byte array and decodes it into CIL instructions. The same thing does .Net platform, when it runs your programm. What ILSpy does is that it tries to guess how did the source code look like, based upon decoded instructions. .NETReflector does the same thing but without using Cecil

netaholic
  • 1,345
  • 10
  • 22