次の方法で共有


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

更新 : 2007 年 11 月

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

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

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

メモ :

リフレクション専用コンテキストは、.NET Framework Version 2.0 で新たに追加されました。

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

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

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

メモ :

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

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

使用例

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

Imports System
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
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);
   }
}
using namespace System;
using namespace System::Reflection;

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);
}

参照

処理手順

方法 : リフレクションのみのコンテキストにアセンブリを読み込む

概念

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

リフレクションの概要

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

参照

ReflectionOnlyLoad

その他の技術情報

共通言語ランタイムのホスト

アプリケーション ドメインの使用