方法: アプリケーション ドメインにアセンブリを読み込む

アプリケーション ドメインにアセンブリを読み込むには、いくつかの方法があります。 推奨されているのは、System.Reflection.Assembly クラスの static (Visual Basic では Shared) Load メソッドを使用する方法です。 それ以外には、以下の方法でアセンブリを読み込むことができます。

  • Assembly クラスの LoadFrom メソッドは、ファイルの場所が指定されたアセンブリを読み込みます。 このメソッドでアセンブリを読み込む場合は、別の読み込みコンテキストが使用されます。

  • ReflectionOnlyLoad メソッドと ReflectionOnlyLoadFrom メソッドは、リフレクション専用コンテキストにアセンブリを読み込みます。 このコンテキストに読み込まれたアセンブリは検査できますが、実行することはできません。その結果、他のプラットフォームをターゲットにしているアセンブリを検査できます。 「方法:リフレクションのみのコンテキストにアセンブリを読み込む」を参照してください。

Note

リフレクションのみのコンテキストは、.NET Framework バージョン 2.0 での新機能です。

  • AppDomain クラスの CreateInstanceCreateInstanceAndUnwrap などのメソッドは、アプリケーション ドメインにアセンブリを読み込むことができます。

  • Type クラスの GetType メソッドは、アセンブリを読み込むことができます。

  • System.AppDomain クラスの Load メソッドは、アセンブリを読み込むことができますが、主に COM の相互運用性のために使用されます。 このメソッドの呼び出し元であるアプリケーション ドメイン以外のアプリケーション ドメインにアセンブリを読み込む場合は、このメソッドを使用しないでください。

Note

.NET Framework バージョン 2.0 以降では、現在読み込まれているランタイムよりバージョン番号が新しい .NET Framework のバージョンでコンパイルされたアセンブリは、ランタイムに読み込まれないようになりました。 これはメジャー コンポーネントとマイナー コンポーネントの組み合わせたバージョン番号に適用されます。

読み込まれたアセンブリの Just-In-Time (JIT) コンパイル コードがアプリケーション ドメイン間で共有される方法を指定できます。 詳細については、「アプリケーション ドメインとアセンブリ」を参照してください。

次に示すコードは、"example.exe" または "example.dll" という名前のアセンブリを現在のアプリケーション ドメインに読み込み、アセンブリから Example という型を取得します。さらに、その型に使用する MethodA というパラメーターを持たないメソッドを取得して、そのメソッドを実行します。 読み込まれたアセンブリから情報を取得する方法の詳細については、「型の動的な読み込みおよび使用」を参照してください。

using namespace System;
using namespace System::Reflection;

public ref class Asmload0
{
public:
    static void Main()
    {
        // Use the file name to load the assembly into the current
        // application domain.
        Assembly^ a = Assembly::Load("example");
        // Get the type to use.
        Type^ myType = a->GetType("Example");
        // Get the method to call.
        MethodInfo^ myMethod = myType->GetMethod("MethodA");
        // Create an instance.
        Object^ obj = Activator::CreateInstance(myType);
        // Execute the method.
        myMethod->Invoke(obj, nullptr);
    }
};

int main()
{
    Asmload0::Main();
}
using System;
using System.Reflection;

public class Asmload0
{
    public static void Main()
    {
        // Use the file name to load the assembly into the current
        // application domain.
        Assembly a = Assembly.Load("example");
        // Get the type to use.
        Type myType = a.GetType("Example");
        // Get the method to call.
        MethodInfo myMethod = myType.GetMethod("MethodA");
        // Create an instance.
        object obj = Activator.CreateInstance(myType);
        // Execute the method.
        myMethod.Invoke(obj, null);
    }
}
Imports System.Reflection

Public Class Asmload0
    Public Shared Sub Main()
        ' Use the file name to load the assembly into the current
        ' application domain.
        Dim a As Assembly = Assembly.Load("example")
        ' Get the type to use.
        Dim myType As Type = a.GetType("Example")
        ' Get the method to call.
        Dim myMethod As MethodInfo = myType.GetMethod("MethodA")
        ' Create an instance.
        Dim obj As Object = Activator.CreateInstance(myType)
        ' Execute the method.
        myMethod.Invoke(obj, Nothing)
    End Sub
End Class

関連項目