次の方法で共有


AppDomain.Load メソッド

現在のアプリケーション ドメインに Assembly を読み込みます。

オーバーロードの一覧

AssemblyName を指定して、 Assembly を読み込みます。

[Visual Basic] Overloads Public Overridable Function Load(AssemblyName) As Assembly Implements _AppDomain.Load

[C#] public virtual Assembly Load(AssemblyName);

[C++] public: virtual Assembly* Load(AssemblyName*);

[JScript] public function Load(AssemblyName) : Assembly;

生成された Assembly を含む COFF (Common Object File Format) ベースのイメージを使用して、 Assembly を読み込みます。

[Visual Basic] Overloads Public Overridable Function Load(Byte()) As Assembly Implements _AppDomain.Load

[C#] public virtual Assembly Load(byte[]);

[C++] public: virtual Assembly* Load(unsigned char __gc[]);

[JScript] public function Load(Byte[]) : Assembly;

表示名を指定して Assembly を読み込みます。

[Visual Basic] Overloads Public Overridable Function Load(String) As Assembly Implements _AppDomain.Load

[C#] public virtual Assembly Load(string);

[C++] public: virtual Assembly* Load(String*);

[JScript] public function Load(String) : Assembly;

AssemblyName を指定して、 Assembly を読み込みます。

[Visual Basic] Overloads Public Overridable Function Load(AssemblyName, Evidence) As Assembly Implements _AppDomain.Load

[C#] public virtual Assembly Load(AssemblyName, Evidence);

[C++] public: virtual Assembly* Load(AssemblyName*, Evidence*);

[JScript] public function Load(AssemblyName, Evidence) : Assembly;

生成された Assembly を含む COFF (Common Object File Format) ベースのイメージを使用して、 Assembly を読み込みます。 Assembly のシンボルを表す生バイトも読み込まれます。

[Visual Basic] Overloads Public Overridable Function Load(Byte(), Byte()) As Assembly Implements _AppDomain.Load

[C#] public virtual Assembly Load(byte[], byte[]);

[C++] public: virtual Assembly* Load(unsigned char __gc[], unsigned char __gc[]);

[JScript] public function Load(Byte[], Byte[]) : Assembly;

表示名を指定して Assembly を読み込みます。

[Visual Basic] Overloads Public Overridable Function Load(String, Evidence) As Assembly Implements _AppDomain.Load

[C#] public virtual Assembly Load(string, Evidence);

[C++] public: virtual Assembly* Load(String*, Evidence*);

[JScript] public function Load(String, Evidence) : Assembly;

生成された Assembly を含む COFF (Common Object File Format) ベースのイメージを使用して、 Assembly を読み込みます。 Assembly のシンボルを表す生バイトも読み込まれます。

[Visual Basic] Overloads Public Overridable Function Load(Byte(), Byte(), Evidence) As Assembly Implements _AppDomain.Load

[C#] public virtual Assembly Load(byte[], byte[], Evidence);

[C++] public: virtual Assembly* Load(unsigned char __gc[], unsigned char __gc[], Evidence*);

[JScript] public function Load(Byte[], Byte[], Evidence) : Assembly;

使用例

[Visual Basic, C#, C++] 生アセンブリの読み込み方法を次の例に示します。

[Visual Basic, C#, C++] このコード例を実行するには、アセンブリの完全限定名を指定する必要があります。アセンブリの完全限定名を取得する方法については、「 アセンブリ名 」を参照してください。

[Visual Basic, C#, C++] メモ   ここでは、Load のオーバーロード形式のうちの 1 つだけについて、使用例を示します。その他の例については、各オーバーロード形式のトピックを参照してください。

 
Imports System
Imports System.IO
Imports System.Reflection
Imports System.Reflection.Emit

Module Test
   
   Sub Main()
      Dim currentDomain As AppDomain = AppDomain.CurrentDomain
      
      InstantiateMyType(currentDomain)      ' Failed!

      AddHandler currentDomain.AssemblyResolve, AddressOf MyResolver
      
      InstantiateMyType(currentDomain)      ' OK!
   End Sub 'Main
   
   
   Sub InstantiateMyType(domain As AppDomain)
      Try
     ' You must supply a valid fully qualified assembly name here.
         domain.CreateInstance("Assembly text name, Version, Culture, PublicKeyToken", "MyType")
      Catch e As Exception
         Console.WriteLine(e.Message)
      End Try
   End Sub 'InstantiateMyType
   
   
   ' Loads the content of a file to a byte array. 
   Function loadFile(filename As String) As Byte()
      Dim fs As New FileStream(filename, FileMode.Open)
      Dim buffer(CInt(fs.Length)) As Byte
      fs.Read(buffer, 0, buffer.Length)
      fs.Close()
      
      Return buffer
   End Function 'loadFile
   
   
   Function MyResolver(sender As Object, args As ResolveEventArgs) As System.Reflection.Assembly
      Dim domain As AppDomain = DirectCast(sender, AppDomain)
      
      ' Once the files are generated, this call is
      ' actually no longer necessary.
      EmitAssembly(domain)
      
      Dim rawAssembly As Byte() = loadFile("temp.dll")
      Dim rawSymbolStore As Byte() = loadFile("temp.pdb")
      Dim myAssembly As System.Reflection.Assembly = domain.Load(rawAssembly, rawSymbolStore)
      
      Return myAssembly
   End Function 'MyResolver
   
   
   ' Creates a dynamic assembly with symbol information
   ' and saves them to temp.dll and temp.pdb
   Sub EmitAssembly(domain As AppDomain)
      Dim assemblyName As New AssemblyName()
      assemblyName.Name = "MyAssembly"
      
      Dim assemblyBuilder As AssemblyBuilder = domain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Save)
      Dim moduleBuilder As ModuleBuilder = assemblyBuilder.DefineDynamicModule("MyModule", "temp.dll", True)
      Dim typeBuilder As TypeBuilder = moduleBuilder.DefineType("MyType", TypeAttributes.Public)
      
      Dim constructorBuilder As ConstructorBuilder = typeBuilder.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, Nothing)
      Dim ilGenerator As ILGenerator = constructorBuilder.GetILGenerator()
      ilGenerator.EmitWriteLine("MyType instantiated!")
      ilGenerator.Emit(OpCodes.Ret)
      
      typeBuilder.CreateType()
      
      assemblyBuilder.Save("temp.dll")
   End Sub 'EmitAssembly

End Module 'Test

[C#] 
using System;
using System.IO;
using System.Reflection;
using System.Reflection.Emit;

class Test {
   public static void Main() {
      AppDomain currentDomain = AppDomain.CurrentDomain;
      
      InstantiateMyType(currentDomain);   // Failed!

      currentDomain.AssemblyResolve += new ResolveEventHandler(MyResolver);
      
      InstantiateMyType(currentDomain);   // OK!
   }
   
   static void InstantiateMyType(AppDomain domain) {
      try {
     // You must supply a valid fully qualified assembly name here.
         domain.CreateInstance("Assembly text name, Version, Culture, PublicKeyToken", "MyType");
      } catch (Exception e) {
         Console.WriteLine(e.Message);
      }
   }
   
   // Loads the content of a file to a byte array. 
   static byte[] loadFile(string filename) {
      FileStream fs = new FileStream(filename, FileMode.Open);
      byte[] buffer = new byte[(int) fs.Length];
      fs.Read(buffer, 0, buffer.Length);
      fs.Close();
   
      return buffer;
   }   

   static Assembly MyResolver(object sender, ResolveEventArgs args) {
      AppDomain domain = (AppDomain) sender;

      // Once the files are generated, this call is
      // actually no longer necessary.
      EmitAssembly(domain);
      
      byte[] rawAssembly = loadFile("temp.dll");
      byte[] rawSymbolStore = loadFile("temp.pdb");
      Assembly assembly = domain.Load(rawAssembly, rawSymbolStore);

      return assembly;
   }
   
   // Creates a dynamic assembly with symbol information
   // and saves them to temp.dll and temp.pdb
   static void EmitAssembly(AppDomain domain) {
      AssemblyName assemblyName = new AssemblyName();
      assemblyName.Name = "MyAssembly";

      AssemblyBuilder assemblyBuilder = domain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Save);
      ModuleBuilder moduleBuilder = assemblyBuilder.DefineDynamicModule("MyModule", "temp.dll", true);
      TypeBuilder typeBuilder = moduleBuilder.DefineType("MyType", TypeAttributes.Public);

      ConstructorBuilder constructorBuilder = typeBuilder.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, null);
      ILGenerator ilGenerator = constructorBuilder.GetILGenerator();
      ilGenerator.EmitWriteLine("MyType instantiated!");
      ilGenerator.Emit(OpCodes.Ret);

      typeBuilder.CreateType();
      
      assemblyBuilder.Save("temp.dll");
   }
}

[C++] 
#using <mscorlib.dll>

using namespace System;
using namespace System::IO;
using namespace System::Reflection;
using namespace System::Reflection::Emit;

void InstantiateMyType(AppDomain* domain) {
   try {
      // You must supply a valid fully qualified assembly name here.
      domain->CreateInstance(S"Assembly text name, Version, Culture, PublicKeyToken", S"MyType");
   } catch (Exception* e) {
      Console::WriteLine(e->Message);
   }
}

// Loads the content of a file to a Byte array.
Byte loadFile(String* filename)[] {
   FileStream* fs = new FileStream(filename, FileMode::Open);
   Byte buffer[] = new Byte[(int) fs->Length];
   fs->Read(buffer, 0, buffer->Length);
   fs->Close();

   return buffer;
}

// Creates a dynamic assembly with symbol information
// and saves them to temp.dll and temp.pdb
void EmitAssembly(AppDomain* domain) {
   AssemblyName* assemblyName = new AssemblyName();
   assemblyName->Name = S"MyAssembly";

   AssemblyBuilder*  assemblyBuilder = domain->DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess::Save);
   ModuleBuilder*  moduleBuilder = assemblyBuilder->DefineDynamicModule(S"MyModule", S"temp.dll", true);
   TypeBuilder*  typeBuilder = moduleBuilder->DefineType(S"MyType", TypeAttributes::Public);

   ConstructorBuilder*  constructorBuilder = typeBuilder->DefineConstructor(MethodAttributes::Public, CallingConventions::Standard, 0);
   ILGenerator*  ilGenerator = constructorBuilder->GetILGenerator();
   ilGenerator->EmitWriteLine(S"MyType instantiated!");
   ilGenerator->Emit(OpCodes::Ret);

   typeBuilder->CreateType();

   assemblyBuilder->Save(S"temp.dll");
}

__gc class Resolver {
public:
   static Assembly* MyResolver(Object* sender, ResolveEventArgs* args) {
      AppDomain*  domain = dynamic_cast<AppDomain*> (sender);

      // Once the files are generated, this call is
      // actually no longer necessary.
      EmitAssembly(domain);

      Byte rawAssembly[] = loadFile(S"temp.dll");
      Byte rawSymbolStore[] = loadFile(S"temp.pdb");
      Assembly*  assembly = domain->Load(rawAssembly, rawSymbolStore);

      return assembly;
   }
};

int main() {
   AppDomain*  currentDomain = AppDomain::CurrentDomain;

   InstantiateMyType(currentDomain);   // Failed!

   currentDomain->AssemblyResolve += new ResolveEventHandler(0, Resolver::MyResolver);

   InstantiateMyType(currentDomain);   // OK!
}

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

参照

AppDomain クラス | AppDomain メンバ | System 名前空間