C# 将 DLL 加载到单独的 AppDomain 中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/88717/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 11:29:44  来源:igfitidea点击:

Loading DLLs into a separate AppDomain

提问by Jon Turner

I want to load one or more DLLs dynamically so that they run with a different security or basepath than my main application. How do I load these DLLs into a separate AppDomain and instantiate objects from them?

我想动态加载一个或多个 DLL,以便它们以与我的主应用程序不同的安全性或基本路径运行。如何将这些 DLL 加载到单独的 AppDomain 中并从中实例化对象?

采纳答案by Jon Turner

More specifically

进一步来说

AppDomain domain = AppDomain.CreateDomain("New domain name");
//Do other things to the domain like set the security policy

string pathToDll = @"C:\myDll.dll"; //Full path to dll you want to load
Type t = typeof(TypeIWantToLoad);
TypeIWantToLoad myObject = (TypeIWantToLoad)domain.CreateInstanceFromAndUnwrap(pathToDll, t.FullName);

If all that goes properly (no exceptions thrown) you now have an instance of TypeIWantToLoad loaded into your new domain. The instance you have is actually a proxy (since the actual object is in the new domain) but you can use it just like your normal object.

如果一切顺利(没有抛出异常),您现在就有一个 TypeIWantToLoad 实例加载到您的新域中。您拥有的实例实际上是一个代理(因为实际对象在新域中),但您可以像使用普通对象一样使用它。

Note: As far as I know TypeIWantToLoad has to inherit from MarshalByRefObject.

注意:据我所知 TypeIWantToLoad 必须从 MarshalByRefObject 继承。

回答by Andy

You can use the AppDomain.CreateInstancemethod to do this. You'll need to call the Unwrap method of the ObjectHandle that is returned to get at the actual object.

您可以使用AppDomain.CreateInstance方法来执行此操作。您需要调用返回的 ObjectHandle 的 Unwrap 方法以获取实际对象。

回答by Andy

If you're targeting 3.5, you can take advantage of the new managed extensibility frameworkto handle all the heavy lifting for you.

如果您的目标是 3.5,您可以利用新的托管可扩展性框架为您处理所有繁重的工作。

回答by Min

Create a new Appdomain with AppDomain.Create( ... ). After creating the AppDomain load the DLLs into that AppDomain.

使用 AppDomain.Create( ... ) 创建一个新的 Appdomain。创建 AppDomain 后,将 DLL 加载到该 AppDomain 中。

Look into all the methods that Appdomain has with Create*. There are certain things like CreateInstanceAndUnwrap, etc.

查看 Appdomain 具有的所有 Create* 方法。有一些东西,比如 CreateInstanceAndUnwrap 等。

回答by Ty.

As previously stated, use AppDomain.CreateDomain to create a new app domain. You can then load an assembly into it using the Load method, or even execute an assembly using the ExecuteAssembly method. You can use GetAssemblies to see if an assembly has already been loaded. Be aware too that you cannot unload an assembly once it's loaded. You will need to unload the domain.

如前所述,使用 AppDomain.CreateDomain 创建新的应用程序域。然后,您可以使用 Load 方法将程序集加载到其中,甚至可以使用 ExecuteAssembly 方法执行程序集。您可以使用 GetAssemblies 查看程序集是否已加载。还要注意,一旦加载程序集,您就无法卸载它。您将需要卸载域。