如何从主 AppDomain 卸载程序集? [英] How to unload an assembly from the primary AppDomain?

查看:29
本文介绍了如何从主 AppDomain 卸载程序集?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何卸载加载到主 AppDomain 中的程序集.

I would like to know how to unload an assembly that is loaded into the main AppDomain.

我有以下代码:

var assembly = Assembly.LoadFrom( FilePathHere );

我需要/希望在我完成后能够卸载这个程序集.

I need/want to be able to unload this assembly when I am done.

感谢您的帮助.

推荐答案

对于 .net 版本 core 3.0 及更高版本:

您现在可以卸载程序集.请注意,.net 核心中不再提供应用程序域.相反,您可以创建一个或多个 AssemblyLoadContext,通过该上下文加载程序集,然后卸载该上下文.请参阅 AssemblyLoadContext,或本教程模拟加载插件然后卸载它.

You can now unload assemblies. Note that appdomains are no longer available in .net core. Instead, you can create one or more AssemblyLoadContext, load your assemblies via that context, then unload that context. See AssemblyLoadContext, or this tutorial that simulates loading a plugin then unloading it.

对于.net core 3之前的.net版本,包括netframework 4及更低版本

您不能从应用程序域卸载程序集.您可以销毁应用程序域,但是一旦将程序集加载到应用程序域中,它就会在应用程序域的整个生命周期内都存在.

You can not unload an assembly from an appdomain. You can destroy appdomains, but once an assembly is loaded into an appdomain, it's there for the life of the appdomain.

参见 Jason Zander 对 为什么没有 Assembly.Unload 方法?

See Jason Zander's explanation of Why isn't there an Assembly.Unload method?

如果您使用的是 3.5,您可以使用 AddIn Framework 来更轻松地管理/调用不同的 AppDomains(您可以卸载,卸载所有程序集).如果您使用的是之前的版本,您需要自己创建一个新的应用程序域来卸载它.

If you are using 3.5, you can use the AddIn Framework to make it easier to manage/call into different AppDomains (which you can unload, unloading all the assemblies). If you are using versions before that, you need to create a new appdomain yourself to unload it.

这篇关于如何从主 AppDomain 卸载程序集?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆