我什么时候会使用 AppDomain? [英] When would I use an AppDomain?
问题描述
我对反射还很陌生,我想知道我会使用(第二个)AppDomain 做什么?在商业应用中会有什么实际应用?
I'm fairly new to reflection and I was wonder what I would use a (second) AppDomain for? What practical application would one have in a business application?
推荐答案
有很多用途.辅助 AppDomain 可以提供某种程度的隔离,类似于操作系统提供的进程隔离.
There are numerous uses. An secondary AppDomain can provide a degree of isolation that is similar to the isolation an OS provides processes.
我使用它的一个实际用途是动态加载插件"DLL.我想支持在主可执行文件启动时扫描 DLL 的目录,加载它们并检查它们的类型以查看是否有任何实现了特定接口(即插件的合同).如果不创建辅助 AppDomain,您将无法卸载可能没有实现所寻求接口的任何类型的 DLL/程序集.您可以创建辅助 AppDomain,在那里加载程序集,然后检查类型,而不是在您的流程中携带额外的程序集和类型等.完成后,您可以删除辅助 AppDomain,从而删除您的类型.
One practical use that I've used it for is dynamically loading "plug-in" DLLs. I wanted to support scanning a directory for DLLs at startup of the main executable, loading them and checking their types to see if any implemented a specific interface (i.e. the contract of the plug-in). Without creating a secondary AppDomain, you have no way to unload a DLL/assembly that may not have any types that implement the interface sought. Rather than carry around extra assemblies and types, etc. in your process, you can create a secondary AppDomain, load the assembly there and then examine the types. When you're done, you can get rid of the secondary AppDomain and thus your types.
这篇关于我什么时候会使用 AppDomain?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!