我想用 ILSpy 反汇编整个 .NET 程序集。
我将此代码用作基础:
http ://skysigal.xact-solutions.com/Blog/tabid/427/entryid/2488/Default.aspx
它工作正常,就在我有一个引用 Npgsql.dll(或任何其他非 gac 程序集)的程序集时,我得到一个 AssemblyResolutionException。
无法解析程序集:'Npgsql,版本 = 2.0.11.92,文化 = 中性,PublicKeyToken = 5d8b90d52f46fda7'
我知道如何获取引用的程序集,但如何将它们添加到 ast ?
// SqlWebAdmin.Models.Decompiler.DecompileAssembly("xy.dll");
public static string DecompileAssembly(string pathToAssembly)
{
//Assembly assembly = Assembly.LoadFrom(pathToAssembly);
System.Reflection.Assembly assembly = System.Reflection.Assembly.ReflectionOnlyLoadFrom(pathToAssembly);
//assembly.GetReferencedAssemblies();
//assembly.GetReferencedAssemblies(assembly);
Mono.Cecil.AssemblyDefinition assemblyDefinition =
Mono.Cecil.AssemblyDefinition.ReadAssembly(pathToAssembly);
ICSharpCode.Decompiler.Ast.AstBuilder astBuilder = new ICSharpCode.Decompiler.Ast.AstBuilder(new ICSharpCode.Decompiler.DecompilerContext(assemblyDefinition.MainModule));
astBuilder.AddAssembly(assemblyDefinition);
//new Helpers.RemoveCompilerAttribute().Run(decompiler.CompilationUnit);
using (System.IO.StringWriter output = new System.IO.StringWriter())
{
astBuilder.GenerateCode(new ICSharpCode.Decompiler.PlainTextOutput(output));
string result = output.ToString();
return result;
}
return "";
} // End Function DecompileAssembly