博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
动态加载和卸载 DLL
阅读量:6896 次
发布时间:2019-06-27

本文共 2013 字,大约阅读时间需要 6 分钟。

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(@"..\..\..\ClassLibrary1\bin\Debug\ClassLibrary1.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 );            }        }    }}

 

转载于:https://www.cnblogs.com/lhyqzx/p/6780109.html

你可能感兴趣的文章
常用JavaScript的高级技巧
查看>>
bzoj 1670: [Usaco2006 Oct]Building the Moat护城河的挖掘
查看>>
mac编辑器vim美化
查看>>
MD5摘要算法简析
查看>>
《30天自制操作系统》学习笔记一
查看>>
Python.tornado.2.tornado.options
查看>>
mysql关于or的索引问题
查看>>
装在u盘的linux
查看>>
ASP.NET几种页面数据绑定的用法及区别: <%#、 <%=、 <% 、<%@
查看>>
zookeeper
查看>>
ABP源码分析二十四:Notification
查看>>
Photo4
查看>>
(八)mybatis之多对多
查看>>
h5空白页面过渡加载
查看>>
端午悲剧—我的上海情结(二)
查看>>
Ajax实现登陆并友好提示错误信息
查看>>
第1周小组博客作业——1702班1组
查看>>
思考线上如何既保证不影响查询,又能做更新操作
查看>>
python 10day--python 的基本数据类型
查看>>
HTTP无状态什么意思?
查看>>