Mono Cecil添加缺失的程序集

时间:2017-06-13 14:29:46

标签: c# .net .net-assembly mono.cecil

出于某种原因,当我尝试加载一些程序集并对其进行分析时,我收到错误Mono.Cecil.AssemblyResolutionException: Failed to resolve assembly...

我真的不在乎为什么会抛出这个异常,我知道丢失的程序集在哪里。

有一种类似于AppDomain.CurrentDomain.AssemblyResolve事件的方法,但对于Mono.Cecil? 我可以手动加载丢失的组件,但我不知道如何 那么,我如何为Mono.Cecil加载程序集?

1 个答案:

答案 0 :(得分:4)

显然Mono.Cecil支持。 当您在AssemblyDefinition.ReadAssembly内加载ReadParameters部件时,可以将AssemblyResolver属性更改为解析器。

创建解析器只需从BaseAssemblyResolver继承,如:

private class CustomResolver : BaseAssemblyResolver
{
    private DefaultAssemblyResolver _defaultResolver;

    public CustomResolver()
    {
        _defaultResolver = new DefaultAssemblyResolver();
    }

    public override AssemblyDefinition Resolve(AssemblyNameReference name)
    {
        AssemblyDefinition assembly;
        try
        {
            assembly = _defaultResolver.Resolve(name);
        }
        catch (AssemblyResolutionException ex)
        {
             assembly = ...; // Your resolve logic   
        }
        return assembly;
    }
}