动态加载和卸载 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(@"..\..\..\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 );
}
}
}
}
动态加载和卸载 DLL的更多相关文章
- c# 动态加载和卸载DLL程序集
原文:c# 动态加载和卸载DLL程序集 在 C++中加载和卸载DLL是一件很容易的事,LoadLibrary和FreeLibrary让你能够轻易的在程序中加载DLL,然后在任何地方卸载.在 C#中我们 ...
- C#中动态加载和卸载DLL
在C++中加载和卸载DLL是一件很容易的事,LoadLibrary和FreeLibrary让你能够轻易的在程序中加载DLL,然后在任何地方卸载.在C#中我们也能使用Assembly.LoadFile实 ...
- C#.Net 如何动态加载与卸载程序集(.dll或者.exe)1----C#中动态加载和卸载DLL
我们知道在C++中加载和卸载DLL是一件很容易的事,LoadLibrary和FreeLibrary让你能够轻易的在程序中加载DLL,然后在任何地方卸载. 在C#中我们也能使用Assembly.Load ...
- C#.Net 如何动态加载与卸载程序集(.dll或者.exe)0-------通过应用程序域AppDomain加载和卸载程序集
本博客中以“C#.Net 如何动态加载与卸载程序集(.dll或者.exe)”开头的都是引用莫问奴归处 微软装配车的大门似乎只为货物装载敞开大门,却将卸载工人拒之门外.车门的钥匙只有一把,若要获得还需要 ...
- C#.Net 如何动态加载与卸载程序集(.dll或者.exe)2----通过应用程序域AppDomain加载和卸载程序集之后,如何再返回原来的主程序域
实现目的:动态加载dll,执行完毕之后可以随时卸载掉,并可以替换这些dll,以在运行中更新dll中的类. 其实就是通过应用程序域AppDomain加载和卸载程序集. 在这方面微软有篇文章http:// ...
- 动态加载EXE和DLL
程序中加载了一个DLL文件,但生成的EXE在脱离了DLL文件后仍然可以 单独使用,这是动态加载DLL技术.即:调用资源中的DLL. 此技术的好处:EXE可以使用DLL中的函数,但不会额外增加一 个DL ...
- C#.Net 如何动态加载与卸载程序集(.dll或者.exe)3---- 动态加载Assembly应用程序
下载 supergraphfiles.exe 示例文件. 应用程序体系结构 在我专攻代码之前,我想谈谈我尝试做的事.您可能记得,SuperGraph 让您从函数列表中进行选择.我希望能够在具体的目录中 ...
- 关于XLL加载项动态加载、卸载的演示及XLL函数自定义类型注册的演示
1.在XLL中,把函数定义成不同的类型,在Excel中的实际效果也不同,具体如下: pxMacroType value ...
- C#.Net 如何动态加载与卸载程序集(.dll或者.exe)4-----Net下的AppDomain编程 [摘录]
最近在对AppDomain编程时遇到了一个问题,卸载AppDomain后,在内存中还保留它加载的DLL的数据,所以即使卸载掉AppDomain,还是无法更新它加载的DLL.看来只有关闭整个进程来更新D ...
随机推荐
- 人工智能: 自动寻路算法实现(四、D、D*算法)
博客转载自:https://blog.csdn.net/kongbu0622/article/details/1871520 据 Drew 所知最短路经算法现在重要的应用有计算机网络路由算法,机器人探 ...
- android adt 最新下载地址23.03
打开这个网址就可以看到adt的详细信息: http://developer.android.com/sdk/installing/installing-adt.html 或者直接在你的eclipse ...
- js获取100个随机数存入数组
. //js获取100个随机数存入数组 $(function () { var arr = []; ; var str = ""; ) { , ); ) { arr[num] = ...
- 轻松学习JavaScript十四:JavaScript的RegExp对象(正則表達式)
一RegExp对象概述 RegExp对象表示正則表達式,RegExp是正則表達式的缩写.它是对字符串运行模式匹配的强大工具. RegExp 对象用于规定在文本中检索的内容. 当您检索某个文本时.能够使 ...
- 用gradle把springboot项目打包成jar
``` 用gradle把springboot项目打包成jar ```### build.gradle 中添加 buildscript { repositories { mavenLocal() mav ...
- <LeetCode OJ> 26 / 264 / 313 Ugly Number (I / II / III)
Write a program to check whether a given number is an ugly number. Ugly numbers are positive numbers ...
- iOS_绘制带删除线的Label
效果图例如以下: 一个带删除线的文本标签,继承自UILabel 自绘代码过程例如以下: 1,重写控件的drawRect方法 2,首先得到上下文对象 3,设置颜色,并指定是填充(Fill)模式还是笔刷( ...
- websocket echo test
http://www.websocket.org/echo.html .net websocket server http://superwebsocket.codeplex.com/ http:// ...
- 121. Best Time to Buy and Sell Stock【easy】
121. Best Time to Buy and Sell Stock[easy] Say you have an array for which the ith element is the pr ...
- cmpp 短信平台
背景: 物联网一般是在设备上安装sim卡,通过2g网络来进行设备与云端系统的交互,网络都是通过移动的基站来进行网络传输的,所以一旦移动的基站有变动,比如流量降级,光缆割接,其他故障登 都会导致2g络的 ...