ILSpy 正在使用 cecil 及其自己的库来完成此任务,我听说 .NETReflector 正在使用他自己的私有算法从 MSIL 生成更高级别的 c#、f#、vb 代码,以及如何在没有依赖关系的情况下执行此操作的任何方法/参考?
编辑:我现在对 ILSpy 和 cecil 作为骨干感到满意
解决方案:
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();
}