C# - 将.NET程序集加载到单独的AppDomain中,以便可以卸载它 [英] C# - Loading .NET Assembly into Separate AppDomain So You Can Unload It
问题描述
可能重复:
将DLL加载到单独的AppDomain中
什么是将.NET程序集加载到单独的AppDomain的正确方法,以便您可以访问其类型/类,但仍然可以卸载它(并重新加载它)。
What is the proper way to load a .NET assembly into a separate AppDomain so you can have access to its Types/Classes but still be able to unload it (and reload it).
这是以前讨论的一个切线:
C# - 正确加载程序集,查找类和调用Run()方法的方法
This is a tangent of this previous discussion: C# - Correct Way to Load Assembly, Find Class and Call Run() Method
推荐答案
基本上,您只需创建新的AppDomain,然后调用 AppDomain .CreateInstanceAndUnwrap 创建类型。以下是关于此过程的简单 CodeProject文章。
Basically, you just create the new AppDomain, and then call AppDomain.CreateInstanceAndUnwrap to create the type. Here's a simple CodeProject article about this process.
这里有一些技巧。你不能直接引用Type(这将把类型加载到你当前的AppDomain中),而且这些对象都应该从 MarshallByRefObj 。这将允许您使用远程对象,这意味着不要将其加载到AppDomain中。
There are some tricks, here. You can't ever refer to the Type directly (this will load the type into your current AppDomain), and the objects should all derive from MarshallByRefObj. This will allow you to use the object "remotely", meaning keep it from loading into your AppDomain.
这篇关于C# - 将.NET程序集加载到单独的AppDomain中,以便可以卸载它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!