5

For some reason when I try to load some assembly and analyze it, I'm getting error Mono.Cecil.AssemblyResolutionException: Failed to resolve assembly...

I don't really care about why this exception throw, I know where is the missing assembly.

There is a way to some something like AppDomain.CurrentDomain.AssemblyResolve event but for Mono.Cecil? I can manually load the missing assembly but I don't know how.
So, how can I load an assembly for Mono.Cecil?

Jester
  • 56,577
  • 4
  • 81
  • 125
nrofis
  • 8,975
  • 14
  • 58
  • 113

2 Answers2

8

Apparently Mono.Cecil support that. When you load the Assembly with AssemblyDefinition.ReadAssembly inside the ReadParameters you can change the AssemblyResolver property to your resolver.

To create a resolver just inherit from BaseAssemblyResolver like:

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;
    }
}
nrofis
  • 8,975
  • 14
  • 58
  • 113
2

In my case I was missing, while working on a .NET Core 3.1 project.

Install-Package Microsoft.Azure.WebJobs.Script.ExtensionsMetadataGenerator -Version 4.0.1

https://www.nuget.org/packages/Microsoft.Azure.WebJobs.Script.ExtensionsMetadataGenerator/

Dwain Browne
  • 792
  • 7
  • 12
  • Yep, this did it for me. I created a new ASP.NET Core 3.1 Azure Functions project. This was my first error. Adding the package fixed it for me. Thank you! – ganders Jul 27 '22 at 16:24