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
AppDomain—or the process containing
it—is 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).
public sealed class AppDomain : MarshalByRefObject, _AppDomain, System.Security.IEvidenceFactory {
// Public Static Properties
public static AppDomain CurrentDomain{get; }
// Public Instance Properties
public string BaseDirectory{get; }
// implements _AppDomain
public string DynamicDirectory{get; }
// implements _AppDomain
public Evidence Evidence{get; }
// implements System.Security.IEvidenceFactory
public string FriendlyName{get; }
// implements _AppDomain
public string RelativeSearchPath{get; }
// implements _AppDomain
public AppDomainSetup SetupInformation{get; }
public bool ShadowCopyFiles{get; }
// implements _AppDomain
// Public Static Methods
public static AppDomain CreateDomain(string friendlyName);
public static AppDomain CreateDomain(string friendlyName, System.Security.Policy.Evidence securityInfo);
public static AppDomain CreateDomain(string friendlyName, System.Security.Policy.Evidence securityInfo,
AppDomainSetup info);
public static AppDomain CreateDomain(string friendlyName, System.Security.Policy.Evidence securityInfo,
string appBasePath, string appRelativeSearchPath, bool shadowCopyFiles);
public static int GetCurrentThreadId( );
public static void Unload(AppDomain domain);
// Public Instance Methods
public void AppendPrivatePath(string path);
// implements _AppDomain
public void ClearPrivatePath( );
// implements _AppDomain
public void ClearShadowCopyPath( );
// implements _AppDomain
public ObjectHandle CreateComInstanceFrom(string assemblyName, string typeName);
public ObjectHandle CreateComInstanceFrom(string assemblyFile, string typeName, byte[ ] hashValue,
System.Configuration.Assemblies.AssemblyHashAlgorithm hashAlgorithm);
public ObjectHandle CreateInstance(string assemblyName, string typeName)
// implements _AppDomain
public ObjectHandle CreateInstance(string assemblyName, string typeName, bool ignoreCase,
System.Reflection.BindingFlags bindingAttr, System.Reflection.Binder binder, object[ ] args,
System.Globalization.CultureInfo culture, object[ ] activationAttributes,
System.Security.Policy.Evidence securityAttributes)
// implements _AppDomain
public ObjectHandle CreateInstance(string assemblyName, string typeName,
object[ ] activationAttributes)
// implements _AppDomain
public object CreateInstanceAndUnwrap(string assemblyName, string typeName);
public object CreateInstanceAndUnwrap(string assemblyName, string typeName, bool ignoreCase,
System.Reflection.BindingFlags bindingAttr, System.Reflection.Binder binder, object[ ] args,
System.Globalization.CultureInfo culture, object[ ] activationAttributes,
System.Security.Policy.Evidence securityAttributes);
public object CreateInstanceAndUnwrap(string assemblyName, string typeName, object[ ] activationAttributes);
public ObjectHandle CreateInstanceFrom(string assemblyFile, string typeName);
// implements _AppDomain
public ObjectHandle CreateInstanceFrom(string assemblyFile, string typeName, bool ignoreCase,
System.Reflection.BindingFlags bindingAttr, System.Reflection.Binder binder, object[ ] args,
System.Globalization.CultureInfo culture, object[ ] activationAttributes,
System.Security.Policy.Evidence securityAttributes)
// implements _AppDomain
public ObjectHandle CreateInstanceFrom(string assemblyFile, string typeName,
object[ ] activationAttributes)
// implements _AppDomain
public object CreateInstanceFromAndUnwrap(string assemblyName, string typeName);
public object CreateInstanceFromAndUnwrap(string assemblyName, string typeName, bool ignoreCase,
System.Reflection.BindingFlags bindingAttr, System.Reflection.Binder binder, object[ ] args,
System.Globalization.CultureInfo culture, object[ ] activationAttributes,
System.Security.Policy.Evidence securityAttributes);
public object CreateInstanceFromAndUnwrap(string assemblyName, string typeName, object[ ] activationAttributes);
public AssemblyBuilder DefineDynamicAssembly(System.Reflection.AssemblyName name,
System.Reflection.Emit.AssemblyBuilderAccess access);
// implements _AppDomain
public AssemblyBuilder DefineDynamicAssembly(System.Reflection.AssemblyName name,
System.Reflection.Emit.AssemblyBuilderAccess access, System.Security.Policy.Evidence evidence)
// implements _AppDomain
public AssemblyBuilder DefineDynamicAssembly(System.Reflection.AssemblyName name,
System.Reflection.Emit.AssemblyBuilderAccess access, System.Security.Policy.Evidence evidence,
System.Security.PermissionSet requiredPermissions, System.Security.PermissionSet optionalPermissions,
System.Security.PermissionSet refusedPermissions);
// implements _AppDomain
public AssemblyBuilder DefineDynamicAssembly(System.Reflection.AssemblyName name,
System.Reflection.Emit.AssemblyBuilderAccess access, System.Security.PermissionSet requiredPermissions,
System.Security.PermissionSet optionalPermissions, System.Security.PermissionSet refusedPermissions);
// implements _AppDomain
public AssemblyBuilder DefineDynamicAssembly(System.Reflection.AssemblyName name,
System.Reflection.Emit.AssemblyBuilderAccess access, string dir);
// implements _AppDomain
public AssemblyBuilder DefineDynamicAssembly(System.Reflection.AssemblyName name,
System.Reflection.Emit.AssemblyBuilderAccess access, string dir, System.Security.Policy.Evidence evidence);
// implements _AppDomain
public AssemblyBuilder DefineDynamicAssembly(System.Reflection.AssemblyName name,
System.Reflection.Emit.AssemblyBuilderAccess access, string dir, System.Security.Policy.Evidence evidence,
System.Security.PermissionSet requiredPermissions, System.Security.PermissionSet optionalPermissions,
System.Security.PermissionSet refusedPermissions);
// implements _AppDomain
public AssemblyBuilder DefineDynamicAssembly(System.Reflection.AssemblyName name,
System.Reflection.Emit.AssemblyBuilderAccess access, string dir, System.Security.Policy.Evidence evidence,
System.Security.PermissionSet requiredPermissions, System.Security.PermissionSet optionalPermissions,
System.Security.PermissionSet refusedPermissions, bool isSynchronized)
// implements _AppDomain
public AssemblyBuilder DefineDynamicAssembly(System.Reflection.AssemblyName name,
System.Reflection.Emit.AssemblyBuilderAccess access, string dir,
System.Security.PermissionSet requiredPermissions, System.Security.PermissionSet optionalPermissions,
System.Security.PermissionSet refusedPermissions)
// implements _AppDomain
public void DoCallBack(CrossAppDomainDelegate callBackDelegate)
// implements _AppDomain
public int ExecuteAssembly(string assemblyFile);
// implements _AppDomain
public int ExecuteAssembly(string assemblyFile,
System.Security.Policy.Evidence assemblySecurity)
// implements _AppDomain
public int ExecuteAssembly(string assemblyFile, System.Security.Policy.Evidence assemblySecurity,
string[ ] args);
// implements _AppDomain
public int ExecuteAssembly(string assemblyFile, System.Security.Policy.Evidence assemblySecurity,
string[ ] args, byte[ ] hashValue, System.Configuration.Assemblies.AssemblyHashAlgorithm hashAlgorithm);
public Assembly[ ] GetAssemblies( );
// implements _AppDomain
public object GetData(string name);
// implements _AppDomain
public Type GetType( );
// overrides object
public override object InitializeLifetimeService( );
// overrides MarshalByRefObject
public bool IsFinalizingForUnload( );
public Assembly Load(System.Reflection.AssemblyName assemblyRef)
// implements _AppDomain
public Assembly Load(System.Reflection.AssemblyName assemblyRef,
System.Security.Policy.Evidence assemblySecurity);
// implements _AppDomain
public Assembly Load(byte[ ] rawAssembly);
// implements _AppDomain
public Assembly Load(byte[ ] rawAssembly, byte[ ] rawSymbolStore);
// implements _AppDomain
public Assembly Load(byte[ ] rawAssembly, byte[ ] rawSymbolStore, System.Security.Policy.Evidence securityEvidence);
// implements _AppDomain
public Assembly Load(string assemblyString);
// implements _AppDomain
public Assembly Load(string assemblyString, System.Security.Policy.Evidence assemblySecurity)
// implements _AppDomain
public void SetAppDomainPolicy(System.Security.Policy.PolicyLevel domainPolicy)
// implements _AppDomain
public void SetCachePath(string path);
// implements _AppDomain
public void SetData(string name, object data);
// implements _AppDomain
public void SetDynamicBase(string path);
public void SetPrincipalPolicy(System.Security.Principal.PrincipalPolicy policy)
// implements _AppDomain
public void SetShadowCopyFiles( );
public void SetShadowCopyPath(string path);
// implements _AppDomain
public void SetThreadPrincipal(System.Security.Principal.IPrincipal principal)
// implements _AppDomain
public override string ToString( );
// overrides object
// Events
public event AssemblyLoadEventHandler AssemblyLoad;
// implements _AppDomain
public event ResolveEventHandler AssemblyResolve;
// implements _AppDomain
public event EventHandler DomainUnload;
// implements _AppDomain
public event EventHandler ProcessExit;
// implements _AppDomain
public event ResolveEventHandler ResourceResolve;
// implements _AppDomain
public event ResolveEventHandler TypeResolve;
// implements _AppDomain
public event UnhandledExceptionEventHandler UnhandledException;
// implements _AppDomain
}