当前位置:网站首页>C# 动态加载卸载 DLL
C# 动态加载卸载 DLL
2022-08-04 13:47:00 【林德熙】
我最近做的软件,需要检测dll或exe是否混淆,需要反射获得类名,这时发现,C#可以加载DLL,但不能卸载DLL。于是在网上找到一个方法,可以动态加载DLL,不使用时可以卸载。
我在写一个WPF 程序,发现可以通过 Assembly.Load
加载 DLL,但是如何卸载DLL?下面就来说下如何卸载。
看到 Assembly.Load
是把 DLL 加载到当前程序集,这句话,我就想到了我们的主程序集和当前的不同,那么可以加载到当前不会影响主程序。那么如何新建一个程序集?他是否可以卸载,答案是可以的。
首先,我们可以通过var appDomain = AppDomain.CreateDomain(appDomainName);
创建 AppDomain 。他是可以卸载,卸载 AppDomain 使用 AppDomain.Unload
,就可以把加载在 AppDomain 的 DLL 卸载。
于是我们需要把 DLL 加载在 AppDomain ,这样之后可以卸载 AppDomain 动态删掉 加载的DLL。
如果要把 DLL 加载在 AppDomain 需要先写一个类,继承MarshalByRefObject
internal class ApplicationProxy : MarshalByRefObject
{
public void DoSomething()
{
}
}
var proxy =
appDomain.CreateInstanceAndUnwrap(Assembly.GetAssembly(typeof(ApplicationProxy)).FullName,
typeof(ApplicationProxy).ToString()) as ApplicationProxy;
我们可以在 DoSomething 函数加载 DLL ,加载的 DLL 在 AppDomain ,不在主程序,所以卸载 AppDomain 可以卸载 DLL
假如是从 文件加载,可以使用 LoadFile
var assembly = Assembly.LoadFile(file.FullName);
assembly 可以获得所有的类和方法。
然后需要卸载时,可以使用 AppDomain.Unload(appDomain);
建议写var assembly = Assembly.LoadFile(file.FullName);
在 try,写 AppDomain.Unload(appDomain);
在 finally
上面的 appDomainName 是我自己给他的。
http://stackoverflow.com/questions/2132649/loading-unloading-assembly-in-different-appdomain
我们可以验证,如果不使用新建一个 AppDomain 加载的 DLL 会在主程序集,如果使用了,就会在我们新建的 AppDomain 。
首先我们使用 Assembly.LoadFile(file)
加载,再用反射获得当前程序集,然后获取他的所有 type ,当然我们是知道加载的 File 包含的 type,一会可以验证使用已经加载他。
System.Reflection.Assembly.LoadFile(file);
foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
{
//查看type
}
可以看到 file 包含的 type 在主程序。
我们使用新建 appDomain
const string appDomainName = "ConfuseChecker";
var appDomain = AppDomain.CreateDomain(appDomainName);
var proxy =
appDomain.CreateInstanceAndUnwrap(Assembly.GetAssembly(typeof(ApplicationProxy)).FullName,
typeof(ApplicationProxy).ToString()) as ApplicationProxy;
proxy.DoSomething(new FileInfo(file));
AppDomain.Unload(appDomain);
这时可以看到,我们的主程序没有包含 file 的 type 。
边栏推荐
- Ultra-QuickSort
- router---mode
- Lecture 4 SVN
- 博途200/1500PLC多段曲线控温FB(支持40段控温曲线、段曲线搜索、暂停、跳段等功能)
- 【WeChat Mini Program】Social Internship Production Project for Information Management and Information System Major--Trash Fingerprint
- 【牛客刷题-SQL大厂面试真题】NO5.某宝店铺分析(电商模式)
- 荧光磷脂PEG衍生物之一磷脂-聚乙二醇-荧光素,Fluorescein-PEG-DSPE
- 谁说 Mysql 单表最大 2000 W ?我硬要塞它 1 个亿
- router---动态路由匹配
- SLAM 04.视觉里程计-1-相机模型
猜你喜欢
Interviewer: How to view files containing abc string in /etc directory?
用过Apifox这个API接口工具后,确实感觉postman有点鸡肋......
Execution failed for task ‘:xxx:generateReleaseRFile‘.
一文梳理NLP主要模型发展脉络
两款移相振荡器的对比
永磁同步电机FOC驱动代码讲解
PMP每日一练 | 考试不迷路-8.4(包含敏捷+多选)
SCA兼容性分析工具(ORACLE/MySQL/DB2--->MogDB/openGauss/PostgreSQL)
《社会企业开展应聘文职人员培训规范》团体标准在新华书店上架
zabbix自定义图形
随机推荐
vcl啥意思_oval
相似文本聚类与调参
router---路由守卫
文字编码 - Markdown 简明教程
数据库的基本概念
Week 7 Latent Variable Models and Expectation Maximization
leetcode 48. Rotate Image 旋转图像(Medium)
(记录)异步并发,多线程处理表的统计
Interviewer: How to view files containing abc string in /etc directory?
工具函数---字符串处理
router---模式
牛客网刷题记录 || 链表
This article sorts out the development of the main models of NLP
21天学习挑战赛--第二天打卡(setSystemUiVisibility、导航栏、状态栏)
并发刺客(False Sharing)——并发程序的隐藏杀手
idea permanent activation tutorial (new version)
oracle+RAC+linux5.1所需要安装的包
如何才能有效、高效阅读?猿辅导建议“因材因时施教”
AVR学习笔记之熔丝位
正确使用Impala的invalidate metadata与refresh语句