ILSpy,如何解决依赖关系? [英] ILSpy, how to resolve dependencies?

查看:248
本文介绍了ILSpy,如何解决依赖关系?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用ILSpy分解整个.NET程序集.

I want to disassemble an entire .NET assembly with ILSpy.

我将此代码用作基础:
http://skysigal.xact-solutions.com/Blog/tabid/427/entryid/2488/Default.aspx

I used this code as base:
http://skysigal.xact-solutions.com/Blog/tabid/427/entryid/2488/Default.aspx

它运行良好,仅当我有一个引用Npgsql.dll的程序集(或任何其他非gac程序集)时,我才得到AssemblyResolutionException.

And it works fine, just when I have an assembly that references Npgsql.dll (or any other non-gac assembly), then I get an AssemblyResolutionException.

无法解析程序集:'Npgsql,Version = 2.0.11.92,Culture = neutral,PublicKeyToken = 5d8b90d52f46fda7'

Failed to resolve assembly: 'Npgsql, Version=2.0.11.92, Culture=neutral, PublicKeyToken=5d8b90d52f46fda7'

我知道如何获取引用的程序集,但是如何将它们添加到ast中呢?

I know how I can get the referenced assemblies, but how can I add them to 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

推荐答案

您需要告诉Cecil,ILSpy正在使用的基础元数据读取器以及程序集所在的位置.您可以编写:

You need to tell Cecil, the underlying metadata reader that ILSpy is using, where your assemblies are. You can write:

var resolver = new DefaultAssemblyResolver();
resolver.AddSearchDirectory("path/to/my/assemblies");

var parameters = new ReaderParameters
{
    AssemblyResolver = resolver,
};

var assembly = AssemblyDefinition.ReadAssembly(pathToAssembly, parameters);

这是告诉Cecil在哪里解析引用的程序集的最自然的方法.这样,您可以删除使用System.Reflection加载程序集的行,而仅使用ILSpy堆栈.

This is the most natural way to tell Cecil where to resolve referenced assemblies. This way you can remove the line where you load the assembly using System.Reflection, and only use the ILSpy stack.

这篇关于ILSpy,如何解决依赖关系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆