且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

什么是 .NET 应用程序域?

更新时间:2022-11-06 15:06:47

An AppDomain 基本上提供了一个隔离区域,在该区域中代码在进程内部运行.

An AppDomain basically provides an isolated region in which code runs inside of a process.

一种简单的方式来思考它几乎就像一个位于主进程中的轻量级进程.每个 AppDomain 完全隔离地存在于一个进程中,这使您可以安全地运行代码(如果需要,可以在不拆除整个进程的情况下卸载它),具有单独的安全性等.

An easy way to think of it is almost like a lighter-weight process sitting inside of your main process. Each AppDomain exists within a process in complete isolation, which allows you to run code safely (it can be unloaded without tearing down the whole process if needed), with separate security, etc.

至于您的具体情况 - 如果您在一个进程内的 2 个不同 AppDomain 中运行代码,则代码将单独运行.AppDomains 之间的任何通信都将通过 MarshallByRefObject 进行序列化或处理.在这方面,它的行为非常类似于使用远程处理.这提供了极大的安全性 - 您可以运行您不信任的代码,如果它出现错误,也不会影响您.

As to your specifics - if you run code in 2 different AppDomains within a process, the code will run in isolation. Any communication between the AppDomains will get either serialized or handled via MarshallByRefObject. It behaves very much like using remoting in this regard. This provides a huge amount of security - you can run code that you don't trust, and if it does something wrong, it will not affect you.

MSDN 对应用程序域的描述中有更多详细信息.

There are many more details in MSDN's description of Application Domains.