AppDomain クラス
アセンブリ: mscorlib (mscorlib.dll 内)

<ClassInterfaceAttribute(ClassInterfaceType.None)> _ <ComVisibleAttribute(True)> _ Public NotInheritable Class AppDomain Inherits MarshalByRefObject Implements _AppDomain, IEvidenceFactory
[ClassInterfaceAttribute(ClassInterfaceType.None)] [ComVisibleAttribute(true)] public sealed class AppDomain : MarshalByRefObject, _AppDomain, IEvidenceFactory
[ClassInterfaceAttribute(ClassInterfaceType::None)] [ComVisibleAttribute(true)] public ref class AppDomain sealed : public MarshalByRefObject, _AppDomain, IEvidenceFactory

アプリケーション ドメインは、AppDomain オブジェクトで表され、マネージ コードを実行するための分離境界、アンロード境界、およびセキュリティ境界を示します。
-
アプリケーション ドメインを使用すると、プロセスの停止を引き起こす可能性のあるタスクを分離できます。タスクを実行している AppDomain の状態が不安定になった場合でも、プロセスに影響を及ぼすことなく AppDomain をアンロードできます。このことは、プロセスを再起動させることなく長時間にわたって実行する必要がある場合に重要となります。また、データを共有するべきでないタスクも、アプリケーション ドメインを使用して分離できます。
-
既定のアプリケーション ドメインにロードされたアセンブリは、プロセスの実行中にメモリからアンロードすることはできません。しかし、アプリケーション ドメインをもう 1 つ開いてアセンブリをロードおよび実行すると、そのアプリケーション ドメインがアンロードされたときに、アセンブリもアンロードされます。このテクニックを使用すると、大きな DLL を使用する場合のある、長時間実行されるプロセスのワーキング セットを最小限に抑えることができます。
単一のプロセスで複数のアプリケーション ドメインを実行できますが、アプリケーション ドメインとスレッドの間に一対一の相関関係はありません。複数のスレッドが単一のアプリケーション ドメインに属すことができ、特定のスレッドが単一のアプリケーション ドメインに限定されていない場合でも、1 つのスレッドは単一のアプリケーション ドメインで実行されます。
アプリケーション ドメインは、CreateDomain メソッドを使用して作成されます。AppDomain のインスタンスを使用して、アセンブリ (Assembly) を読み込んで実行します。AppDomain は、不要になった場合はアンロードできます。
AppDomain クラスは、アセンブリが読み込まれたり、アプリケーション ドメインがアンロードされたり、処理されない例外がスローされたりしたときにアプリケーションが応答できるように、一連のイベントを実装しています。
アプリケーション ドメインの使い方の詳細については、「アプリケーション ドメイン」を参照してください。
このクラスは、MarshalByRefObject インターフェイス、_AppDomain インターフェイス、および IEvidenceFactory インターフェイスを実装しています。
AppDomain オブジェクトのリモート対応ラッパーは作成しないでください。AppDomain のリモート参照が公開されるため、CreateInstance などのメソッドがリモートで公開され、AppDomain のコード アクセス セキュリティが破壊されてしまう可能性があります。悪意のあるクライアントがリモートの AppDomain に接続すると、AppDomain 自体がアクセスできるすべてのリソースに対してアクセスできるようになります。MarshalByRefObject を拡張し、悪意のあるクライアントがセキュリティ システムをバイパスするために使用できるメソッドを実装している型に対しては、リモート対応ラッパーは作成しないでください。
![]() |
---|
AppDomainSetup.DisallowCodeDownload プロパティの既定値は false です。この設定は、サービスに対して安全ではありません。信頼性が一部しか確認されていないコードをダウンロードするサービスを防止するには、このプロパティを true に設定します。 |
Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows CE プラットフォームメモ : 複数のアプリケーション ドメインで使用するためにアセンブリをドメインに中立なコード領域に読み込むことはできません。

この例では、新しい AppDomain を作成し、その新しい AppDomain で型をインスタンス化して、その型のオブジェクトと通信する方法を示しています。また、この例では、AppDomain をアンロードして、オブジェクトがガベージ コレクタによって収集されるようにする方法を示しています。
Imports System Imports System.Reflection Imports System.Threading Module Module1 Sub Main() ' Get and display the friendly name of the default AppDomain. Dim callingDomainName As String = Thread.GetDomain().FriendlyName Console.WriteLine(callingDomainName) ' Get and display the full name of the EXE assembly. Dim exeAssembly As String = [Assembly].GetEntryAssembly().FullName Console.WriteLine(exeAssembly) ' Construct and initialize settings for a second AppDomain. Dim ads As New AppDomainSetup() ads.ApplicationBase = _ "file:///" + System.Environment.CurrentDirectory ads.DisallowBindingRedirects = False ads.DisallowCodeDownload = True ads.ConfigurationFile = _ AppDomain.CurrentDomain.SetupInformation.ConfigurationFile ' Create the second AppDomain. Dim ad2 As AppDomain = AppDomain.CreateDomain("AD #2", Nothing, ads) ' Create an instance of MarshalbyRefType in the second AppDomain. ' A proxy to the object is returned. Dim mbrt As MarshalByRefType = CType( _ ad2.CreateInstanceAndUnwrap(exeAssembly, _ GetType(MarshalByRefType).FullName), MarshalByRefType) ' Call a method on the object via the proxy, passing the default ' AppDomain's friendly name in as a parameter. mbrt.SomeMethod(callingDomainName) ' Unload the second AppDomain. This deletes its object and ' invalidates the proxy object. AppDomain.Unload(ad2) Try ' Call the method again. Note that this time it fails because ' the second AppDomain was unloaded. mbrt.SomeMethod(callingDomainName) Console.WriteLine("Sucessful call.") Catch e As AppDomainUnloadedException Console.WriteLine("Failed call; this is expected.") End Try End Sub End Module ' Because this class is derived from MarshalByRefObject, a proxy ' to a MarshalByRefType object can be returned across an AppDomain ' boundary. Public Class MarshalByRefType Inherits MarshalByRefObject ' Call this method via a proxy. Public Sub SomeMethod(ByVal callingDomainName As String) ' Get this AppDomain's settings and display some of them. Dim ads As AppDomainSetup = AppDomain.CurrentDomain.SetupInformation Console.WriteLine("AppName={0}, AppBase={1}, ConfigFile={2}", _ ads.ApplicationName, ads.ApplicationBase, ads.ConfigurationFile) ' Display the name of the calling AppDomain and the name ' of the second domain. ' NOTE: The application's thread has transitioned between ' AppDomains. Console.WriteLine("Calling from '{0}' to '{1}'.", _ callingDomainName, Thread.GetDomain().FriendlyName) End Sub End Class 'This code produces output similar to the following: ' ' AppDomainX.exe ' AppDomainX, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null ' AppName=, AppBase=C:\AppDomain\bin, ConfigFile=C:\AppDomain\bin\AppDomainX.exe.config ' Calling from 'AppDomainX.exe' to 'AD #2'. ' Failed call; this is expected.
using System; using System.Reflection; using System.Threading; class Module1 { public static void Main() { // Get and display the friendly name of the default AppDomain. string callingDomainName = Thread.GetDomain().FriendlyName; Console.WriteLine(callingDomainName); // Get and display the full name of the EXE assembly. string exeAssembly = Assembly.GetEntryAssembly().FullName; Console.WriteLine(exeAssembly); // Construct and initialize settings for a second AppDomain. AppDomainSetup ads = new AppDomainSetup(); ads.ApplicationBase = "file:///" + System.Environment.CurrentDirectory; ads.DisallowBindingRedirects = false; ads.DisallowCodeDownload = true; ads.ConfigurationFile = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile; // Create the second AppDomain. AppDomain ad2 = AppDomain.CreateDomain("AD #2", null, ads); // Create an instance of MarshalbyRefType in the second AppDomain. // A proxy to the object is returned. MarshalByRefType mbrt = (MarshalByRefType) ad2.CreateInstanceAndUnwrap( exeAssembly, typeof(MarshalByRefType).FullName ); // Call a method on the object via the proxy, passing the // default AppDomain's friendly name in as a parameter. mbrt.SomeMethod(callingDomainName); // Unload the second AppDomain. This deletes its object and // invalidates the proxy object. AppDomain.Unload(ad2); try { // Call the method again. Note that this time it fails // because the second AppDomain was unloaded. mbrt.SomeMethod(callingDomainName); Console.WriteLine("Sucessful call."); } catch(AppDomainUnloadedException) { Console.WriteLine("Failed call; this is expected."); } } } // Because this class is derived from MarshalByRefObject, a proxy // to a MarshalByRefType object can be returned across an AppDomain // boundary. public class MarshalByRefType : MarshalByRefObject { // Call this method via a proxy. public void SomeMethod(string callingDomainName) { // Get this AppDomain's settings and display some of them. AppDomainSetup ads = AppDomain.CurrentDomain.SetupInformation; Console.WriteLine("AppName={0}, AppBase={1}, ConfigFile={2}", ads.ApplicationName, ads.ApplicationBase, ads.ConfigurationFile ); // Display the name of the calling AppDomain and the name // of the second domain. // NOTE: The application's thread has transitioned between // AppDomains. Console.WriteLine("Calling from '{0}' to '{1}'.", callingDomainName, Thread.GetDomain().FriendlyName ); } } /* This code produces output similar to the following: AppDomainX.exe AppDomainX, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null AppName=, AppBase=C:\AppDomain\bin, ConfigFile=C:\AppDomain\bin\AppDomainX.exe.config Calling from 'AppDomainX.exe' to 'AD #2'. Failed call; this is expected. */
using namespace System; using namespace System::Reflection; using namespace System::Threading; using namespace System::Security::Policy; // Because this class is derived from MarshalByRefObject, a proxy // to a MarshalByRefType object can be returned across an AppDomain // boundary. ref class MarshalByRefType : MarshalByRefObject { public: // Call this method via a proxy. void SomeMethod(String^ callingDomainName) { // Get this AppDomain's settings and display some of them. AppDomainSetup^ ads = AppDomain::CurrentDomain->SetupInformation; Console::WriteLine("AppName={0}, AppBase={1}, ConfigFile={2}", ads->ApplicationName, ads->ApplicationBase, ads->ConfigurationFile ); // Display the name of the calling AppDomain and the name // of the second domain. // NOTE: The application's thread has transitioned between // AppDomains. Console::WriteLine("Calling from '{0}' to '{1}'.", callingDomainName, Thread::GetDomain()->FriendlyName ); }; }; void main() { // Get and display the friendly name of the default AppDomain. String^ callingDomainName = Thread::GetDomain()->FriendlyName; Console::WriteLine(callingDomainName); // Get and display the full name of the EXE assembly. String^ exeAssembly = Assembly::GetEntryAssembly()->FullName; Console::WriteLine(exeAssembly); // Construct and initialize settings for a second AppDomain. AppDomainSetup^ ads = gcnew AppDomainSetup(); ads->ApplicationBase = "file:///" + System::Environment::CurrentDirectory; ads->DisallowBindingRedirects = false; ads->DisallowCodeDownload = true; ads->ConfigurationFile = AppDomain::CurrentDomain->SetupInformation->ConfigurationFile; // Create the second AppDomain. AppDomain^ ad2 = AppDomain::CreateDomain("AD #2", AppDomain::CurrentDomain->Evidence, ads); // Create an instance of MarshalbyRefType in the second AppDomain. // A proxy to the object is returned. MarshalByRefType^ mbrt = (MarshalByRefType^) ad2->CreateInstanceAndUnwrap( exeAssembly, MarshalByRefType::typeid->FullName ); // Call a method on the object via the proxy, passing the // default AppDomain's friendly name in as a parameter. mbrt->SomeMethod(callingDomainName); // Unload the second AppDomain. This deletes its object and // invalidates the proxy object. AppDomain::Unload(ad2); try { // Call the method again. Note that this time it fails // because the second AppDomain was unloaded. mbrt->SomeMethod(callingDomainName); Console::WriteLine("Sucessful call."); } catch(AppDomainUnloadedException^) { Console::WriteLine("Failed call; this is expected."); } } /* This code produces output similar to the following: AppDomainX.exe AppDomainX, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null AppName=, AppBase=C:\AppDomain\bin, ConfigFile=C:\AppDomain\bin\AppDomainX.exe.config Calling from 'AppDomainX.exe' to 'AD #2'. Failed call; this is expected. */

System.MarshalByRefObject
System.AppDomain


Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


- AppDomain クラスのページへのリンク