C# - 加载.NET程序集到单独的AppDomain中,因此您可以卸载它您可以、加载、程序、NET

2023-09-04 01:17:38 作者:终点也是起点

可能重复:   加载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).

这是这个previous讨论的切线: 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 以创建类型。关于这下面是一个简单的 $ C $的CProject文章流程。

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.

有一些技巧,在这里。你永远不能引用类型直接(这将加载类到当前的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.