AppDomain

AppDomain

CF 1.0, ECMA 1.0, marshal by reference

System (mscorlib.dll)

sealed class

This class represents an abstract
separation within the executing
process, which mimics the separation between processes running on a
single machine. As a result, a single .NET process can host multiple
other processes that offer the isolation found between processes,
while keeping the low overhead of a single process.

Every .NET process created has at least one
AppDomain, even when running a simple command
shell-driven application, such as Hello, world,
created by the shim code at the start of a .NET executable file.
Applications that act as containers, however, can create multiple
AppDomains, loading assemblies into each
AppDomain independently of one another. This is,
in fact, precisely how ASP.NET keeps multiple web applications
separate from one another, so that an exception thrown from within
one won't tear down the entire IIS process.

Creating a new AppDomain involves using the static
CreateDomain( ) method. This method is overloaded
four ways, but the most common use is simply to pass in a friendly
name for the AppDomain. When finished with a given
AppDomain, use the Unload( )
method to close down the AppDomain and all objects
stored within it. Should a .NET programmer wish to obtain a reference
to the AppDomain she is currently executing
within, the static property CurrentDomain returns
the current AppDomain.

Each AppDomain contains an entirely separate list
of loaded assemblies accessible via the GetAssemblies(
) method, which returns the list of assemblies loaded for
this particular AppDomain.
AppDomains can also create instances of types
within the given AppDomain, using the
CreateInstance( ) family of methods. An
AppDomain can also load and execute the entry
point of an assembly using the ExecuteAssembly( )
method, or it can load an assembly directly using one of the
Load( ) methods. AppDomains
even support the ability to create dynamic (that is, transient or
temporary) assemblies through the DefineDynamicAssembly(
) method.

AppDomains also offer a number of .NET events for
interested consumers, notifying .NET programmers when an assembly has
been loaded (AssemblyLoad), when an exception has
been thrown out of a thread within that assembly
(UnhandledException), or when the
AppDomainor the process containing
itis being unloaded and torn down
(DomainUnload and ProcessExit).
.NET programmers can use these events to perform necessary actions,
such as loading an assembly from an alternative location when an
assembly fails to load (AssemblyResolve).

AppDomain also contains a number of properties,
which act in a role similar to environment variables within a
process. These AppDomain properties are, like
environment variables, simple name-value mapping pairs, retrievable
in one of two ways: via the GetData( ) method or
via a set of predefined properties on the
AppDomain class (such as
BaseDirectory).