dotnet dynamically load and unload dll code

Three code segments are in the same project

d1.dll is another project

1. Call

  string zfile = AppDomain.CurrentDomain.BaseDirectory + @"\d1.dll";
            LocalLoader ll = new LocalLoader();
            ll.LoadAssembly(zfile);
            ll.Exec("d1.action1" ,"docmd", new object[] { "try" });
            
            
            ll.Unload();
            ll = null;
            GC.Collect();
            GC.WaitForPendingFinalizers();
            GC.Collect(0);

  

2.LocalLoader class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.IO;
namespace testdomain
{

    public class LocalLoader
    {
        private AppDomain appDomain;
        private RemoteLoader remoteLoader;

        public LocalLoader()
        {
            AppDomainSetup setup = new AppDomainSetup();
            setup.ApplicationName = "Test";
            setup.ShadowCopyFiles = "true";
            //setup.ApplicationBase = AppDomain.CurrentDomain.BaseDirectory;
            //setup.PrivateBinPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "private");
            //setup.CachePath = setup.ApplicationBase;
          
            //setup.ShadowCopyDirectories = setup.ApplicationBase;

            appDomain = AppDomain.CreateDomain("TestDomain", null, setup);
            string name = Assembly.GetExecutingAssembly().GetName().FullName;
            remoteLoader = (RemoteLoader)appDomain.CreateInstanceAndUnwrap(
            name,
            typeof(RemoteLoader).FullName);
        }

        public void LoadAssembly(string fullName)
        {
            remoteLoader.LoadAssembly(fullName);
        }

        public void Exec(string typename ,string method_name,object [] param_list)
        {
             remoteLoader.Exec(typename,method_name, param_list);
        }

        public void Unload()
        {
            AppDomain.Unload(appDomain);
            appDomain = null;
        }

        public string FullName
        {
            get
            {
                return remoteLoader.FullName;
            }
        }
    }  

}

  

3.RemoteLoader

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
namespace testdomain
{
    class RemoteLoader:MarshalByRefObject
    {
        private Assembly assembly;

        public void LoadAssembly(string fullName)
        {
            assembly = Assembly.LoadFrom(fullName);
        }

        public void Exec(string typename,string method_name ,object[] param_list)
        {
            object o = assembly.CreateInstance(typename);
            o.GetType().InvokeMember(method_name, System.Reflection.BindingFlags.InvokeMethod, null, o, param_list);
             
        }

        public string FullName
        {
            get { return assembly.FullName; }
        }  
    }
}

  

Guess you like

Origin www.cnblogs.com/coolyylu/p/12551199.html