zoukankan      html  css  js  c++  java
  • 动态加载和卸载 DLL

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ClassLibrary1
    {
        public class Class1
        {
            public static void DoStuff(string msg)
            {
                Console.WriteLine("Class1.DoStuff: " + msg);
                
            }
        }
    }

    上面是调用的DLL源码

    调用主程序源码如下:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Reflection;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                AppDomain ad = AppDomain.CreateDomain("Test");
    
                // Loader lives in another AppDomain
                Loader loader = (Loader)ad.CreateInstanceAndUnwrap(
                    typeof(Loader).Assembly.FullName,
                    typeof(Loader).FullName);
    
                loader.LoadAssembly(@"......ClassLibrary1inDebugClassLibrary1.dll");
                loader.ExecuteStaticMethod(
                    "ClassLibrary1.Class1",
                    "DoStuff",
                    DateTime.Now.ToShortDateString());
                AppDomain.Unload(ad);
                Console.ReadLine();
                
            }
            class Loader : MarshalByRefObject 
            {
                private Assembly _assembly;
    
                public override object InitializeLifetimeService() {
                    return null;
                }
    
                public void LoadAssembly( string path ) {
                    _assembly = Assembly.Load( AssemblyName.GetAssemblyName( path ) );
                }
    
                public object ExecuteStaticMethod( string typeName, string methodName, params object[] parameters ) {
                    Type type = _assembly.GetType( typeName );
                    // TODO: this won't work if there are overloads available
                    MethodInfo method = type.GetMethod(
                        methodName,
                        BindingFlags.Static | BindingFlags.Public );
                    return method.Invoke( null, parameters );
                }
            }
        }
    }
  • 相关阅读:
    unigui在阿里云服务器上部署
    nativeexcel将excel导入数据集
    nativeexcel数据集导出excel
    超市收银系统
    cxgrid回车移到下一个单元格
    cxGrid单元格获得输入焦点
    TDiocpCoderTcpServer返回数据记录有条数限制的问题
    Ubuntu -- 配置Nginx和https及frp
    REMOTE HOST IDENTIFICATION HAS CHANGED 问题解决
    iOS -- iOS11新特性,如何适配iOS11
  • 原文地址:https://www.cnblogs.com/lhyqzx/p/6780109.html
Copyright © 2011-2022 走看看