Chapter 17 - Assemblies

FolderBasedALC

void Main()
{
  FolderBasedALC alc = new FolderBasedALC (@"C:\YourAssemblyFolder");
  alc.LoadFromAssemblyName (new AssemblyName ("YourAssembly"));

  foreach (Assembly a in alc.Assemblies)
    Console.WriteLine (a.FullName);
}

class FolderBasedALC : AssemblyLoadContext
{
  readonly string _folder;
  public FolderBasedALC (string folder) => _folder = folder;

  protected override Assembly Load (AssemblyName assemblyName)
  {
    // Attempt to find the assembly:
    string targetPath = Path.Combine (_folder, assemblyName.Name + ".dll");

    if (File.Exists (targetPath))
      return LoadFromAssemblyPath (targetPath);   // Load the assembly

    return null;    // We can’t find it – it could be a framework assembly
  }
}

AssemblyDependencyResolver

var resolver = new AssemblyDependencyResolver (@"c:\PathToSomeAssemblyWithDepsDotJson.dll");
string path = resolver.ResolveAssemblyToPath (new AssemblyName ("someDependentAssembly"));

Writing a Plug-in System

// See http://www.albahari.com/nutshell/code.aspx
C# 12 in a Nutshell
Buy from amazon.com Buy print or Kindle edition
Buy from ebooks.com Buy PDF edition
Buy from O'Reilly Read via O'Reilly subscription