我什么时候使用AppDomain? [英] When would I use an AppDomain?

查看:74
本文介绍了我什么时候使用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.

我使用它的一种实际用途是动态加载插件-in 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屋!

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