Chapter 17 - Assemblies


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


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

Writing a Plug-in System

// See
C# 12 in a Nutshell
Buy from Buy print or Kindle edition
Buy from Buy PDF edition
Buy from O'Reilly Read via O'Reilly subscription