次の方法で共有


別のアプリケーション ドメインでのコードの実行 (C# および Visual Basic)

アセンブリがアプリケーション ドメインに読み込まれると、そのアセンブリに含まれるコードを実行できます。 アセンブリを読み込む最も簡単な方法は、AssemblyLoad を使用することです。これを使用すると、現在のアプリケーション ドメインにアセンブリが読み込まれ、アセンブリの既定のエントリ ポイントからコードの実行が開始されます。

アセンブリを別のアプリケーション ドメインに読み込むには、ExecuteAssembly または ExecuteAssemblyByName、あるいはこれらのメソッドをオーバーロードした別のバージョンを使用します。

既定のエントリ ポイント以外から開始されるアセンブリを実行する場合は、リモート アセンブリで MarshalByRefObject から派生する新しい型を定義します。 次に、CreateInstance を使用して、その型のインスタンスをアプリケーションで作成します。

次のコードは、1 つの名前空間と 2 つのクラスから成るアセンブリを作成します。 このコードを Visual Studio の HelloWorldRemote という名前のコンソール アプリケーションに貼り付け、ソリューションをビルドまたは実行してから閉じます。 プロジェクトの obj/Debug フォルダーで HelloWorldRemote.exe ファイルを見つけて、このファイルを C: ドライブにコピーします。

' This module contains code to be called.
Module HelloWorldRemote
    Class RemoteObject
        Inherits System.MarshalByRefObject
        Sub RemoteObject()
            System.Console.WriteLine("Hello, World! (RemoteObject Constructor)")
        End Sub
    End Class
    Sub Main()
        System.Console.WriteLine("Hello, World! (Main method)")
    End Sub
End Module
// This namespace contains code to be called.
namespace HelloWorldRemote
{
    public class RemoteObject : System.MarshalByRefObject
    {
        public RemoteObject()
        {
            System.Console.WriteLine("Hello, World! (RemoteObject Constructor)");
        }
    }
    class Program
    {
        static void Main()
        {
            System.Console.WriteLine("Hello, World! (Main method)");
        }
    }
}

別のアプリケーションからコードにアクセスするには、アセンブリを現在のアプリケーション ドメインに読み込むか、または新しいアプリケーション ドメインを作成し、そこにアセンブリを読み込みます。

Assembly.LoadFrom を使用してアセンブリを現在のアプリケーション ドメインに読み込むには、Assembly.CreateInstance を使用して、RemoteObject クラスのインスタンスをインスタンス化します。 このインスタンス化により、オブジェクト コンストラクターが実行されます。

' Load the assembly into the current appdomain:
Dim newAssembly As System.Reflection.Assembly = System.Reflection.Assembly.LoadFrom("c:\HelloWorldRemote.exe")

' Instantiate RemoteObject:
newAssembly.CreateInstance("HelloWorldRemote.RemoteObject")
// Load the assembly into the current appdomain:
System.Reflection.Assembly newAssembly = System.Reflection.Assembly.LoadFrom(@"c:\HelloWorldRemote.exe");

// Instantiate RemoteObject:
newAssembly.CreateInstance("HelloWorldRemote.RemoteObject");

アセンブリを別のアプリケーション ドメインに読み込む場合は、AppDomain.ExecuteAssembly を使用して既定のエントリ ポイントにアクセスするか、または AppDomain.CreateInstance を使用して RemoteObject クラスのインスタンスを作成します。 インスタンスを作成すると、コンストラクターが実行されます。

注意

Assembly.LoadFrom を使用する場合の欠点については、Assembly.LoadFrom(String) の「解説」を参照してください。

Dim NewAppDomain As System.AppDomain = System.AppDomain.CreateDomain("NewApplicationDomain")

' Load the assembly and call the default entry point:
NewAppDomain.ExecuteAssembly("c:\HelloWorldRemote.exe")

' Create an instance of RemoteObject:
NewAppDomain.CreateInstanceFrom("c:\HelloWorldRemote.exe", "HelloWorldRemote.RemoteObject")
System.AppDomain NewAppDomain = System.AppDomain.CreateDomain("NewApplicationDomain");

// Load the assembly and call the default entry point:
NewAppDomain.ExecuteAssembly(@"c:\HelloWorldRemote.exe");

// Create an instance of RemoteObject:
NewAppDomain.CreateInstanceFrom(@"c:\HelloWorldRemote.exe", "HelloWorldRemote.RemoteObject");

アセンブリをプログラムによって読み込まない場合は、ソリューション エクスプローラー[参照の追加] を使用して HelloWorldRemote.exe というアセンブリを指定します。 C# の場合は using HelloWorldRemote; ディレクティブを追加し、Visual Basic の場合は Imports HelloWorldRemote ステートメントを追加します。 次に、プログラムで RemoteObject 型を使用して、RemoteObject オブジェクトのインスタンスを次の例に示すように宣言します。

' This code creates an instance of RemoteObject, 
' assuming HelloWorldRemote has been added as a reference:
Dim o As HelloWorldRemote.RemoteObject = New HelloWorldRemote.RemoteObject()
// This code creates an instance of RemoteObject, 
// assuming HelloWorldRemote has been added as a reference:
HelloWorldRemote.RemoteObject o = new HelloWorldRemote.RemoteObject();

参照

関連項目

アプリケーション ドメイン (C# および Visual Basic)

概念

C# プログラミング ガイド

アプリケーション ドメインとアセンブリ

アプリケーション ドメインを使用したプログラミング

その他の技術情報

Visual Basic のプログラミング ガイド

アプリケーション ドメイン

アプリケーション ドメインとアセンブリを使用したプログラミング