Using AppDomain in C# to dynamically load and unload dll

How to: Load Assemblies into an Application Domain

public static void Main()

        // Use the file name to load the assembly into the current
        // application domain.
        Assembly a = Assembly.Load("example");
        // Get the type to use.
        Type myType = a.GetType("Example");
        // Get the method to call.
        MethodInfo myMethod = myType.GetMethod("MethodA");
        // Create an instance.
        object obj = Activator.CreateInstance(myType);
        // Execute the method.
        myMethod.Invoke(obj, null);

As for how to unload it, you have to unload the AppDomain itself, see this

AppDomain Temporary = AppDomain.CreateDomain("Temporary");
  Gateway Proxy = 
    (Gateway) Temporary.CreateInstanceAndUnwrap("Shim", "Shim.Gateway");

  Match M = Proxy.LoadAndMatch("Plugin.dll", 
    "Though the tough cough and hiccough, plough them through");  

Leave a Comment