⬅ Bradenkoh.com

Using the Assembly Resolver

#Programming

Resolve Assembly Loads

.NET gives you an AppDomain.AssemblyResolve event which gives you greater control over how assemblies are loaded into the app

So, when you register a handler for the event, the handler is called when an assembly fails to bind at runtime.

So for example, if you are building an Addin for a program and they use an older version of an assembly that you are referencing, you can use this event to load in the newer version for your addin.

The official Microsoft documentation link : Resolve assembly loads | Microsoft Learn

Example

If your addin is using Newtonsoft version 13.0.0 but the program that consumes your addin is using version 6. You can use the Assembly.LoadeFrom function in the AssemblyResolve event to bind the newer version for your addin.

 System.Reflection.Assembly CurrentDomain_AssemblyResolve( object sender, ResolveEventArgs args )
  {
    if( args.Name.Contains( "Newtonsoft" ) )
    {
      string filename = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location );
 
      filename = Path.Combine( filename,"Newtonsoft.Json.dll" );
 
      if( File.Exists( filename ) )
      {
        return System.Reflection.Assembly.LoadFrom( filename );
      }
    }
    return null;
  }
 

  void ExportView3D( View3D view3d, string filename )
  {
    AppDomain.CurrentDomain.AssemblyResolve+= CurrentDomain_AssemblyResolve;
 
    Document doc = view3d.Document;
 
    Va3cExportContext context= new Va3cExportContext( doc, filename );
 
    CustomExporter exporter = new CustomExporter(doc, context );
 
    exporter.IncludeFaces = false;
 
    exporter.ShouldStopOnError = false;
 
    exporter.Export( view3d );
  }

Code taken from The Building Coder: RvtVa3c Assembly Resolver (typepad.com)

This might be useful too
How Runtime Locates and Binds Assemblies