• dotnet动态加载以及卸载dll的代码


    三个代码段都是在同一个项目中

    d1.dll是另一个项目

    1.调用

      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类

    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; }
            }  
        }
    }
    

      

    dotnet dll项目都生成前事件执行bat代码

    @echo off
    cd "C:Program Files (x86)KingdeeK3ERP"
    del k3test.dll
    del k3test_old.dll
    ren k3test.dll k3test_old.dll
    exit 0
    

      

  • 相关阅读:
    TextBox控件
    Button控件
    WinForm调试输出数据
    使用NPIO操作Excel
    窗体类笔记
    this关键字的使用
    51job招聘.Net职位分析(关于大学生就业的一点建议)
    解决线性表的编程问题
    如何在UWP中统一处理不同设备间的页面回退逻辑
    Python带你轻松进行网页爬虫
  • 原文地址:https://www.cnblogs.com/coolyylu/p/12551199.html
Copyright © 2020-2023  润新知