c# – Loading dll not from root directory


I am writing a console program in C # that loads dll files from a specific folder. Each dll has a class that implements the IPlugin interface and inherits from MarshalByRefObject.

It is necessary to take into account 3 nuances:

  1. Must be loaded in different AppDomains
  2. The assembly files are not in the root folder where the exe, but in a separate one, for plugins (Plugins \ plugins1 \ Plugins1.dll and Plugins \ Plugins2 \ Plugins2.dll)
  3. The dll structure is not known in advance. Those. you cannot hardcode the namespace and class as shown below:

     var plugin = (IPlugin)domain.CreateInstanceFromAndUnwrap(pathToDll, "PluginNamespace.Plugin1");

Tried loading through the Load method:

var domain = AppDomain.CreateDomain("NewDomain");
var buffer = File.ReadAllBytes(file);
var assembly = domain.Load(buffer);

This only works for assemblies that are flush with exe (the second condition is not met). For dlls inside directories, the error Could not load file or assembly takes off … I read somewhere that you can use CreateInstanceFromAndUnwrap – it works with it, but you need to transfer the namespace and class from the assembly to it, but I don't know it (3 condition). I just know that it implements the IPlugin interface.

Help, please, tell me is it possible to load assemblies at all under such conditions? If so, how?


Try this:

public class DomainResident : MarshalByRefObject
    public void LoadPluginFrom(string path)
        // тут код из http://ru.stackoverflow.com/a/323396
        // с Assembly.Load

// ...
// не забудьте try/catch вокруг
var domain = AppDomain.CreateDomain(invent_some_NEW_name_here);

var resident = (DomainResident)domain.CreateInstanceAndUnwrap(

Scroll to Top