AppDomain.Load AppDomain.Load AppDomain.Load AppDomain.Load Method

定義

現在のアプリケーション ドメインに Assembly を読み込みます。Loads an Assembly into this application domain.

オーバーロード

Load(Byte[]) Load(Byte[]) Load(Byte[]) Load(Byte[])

生成された Assembly を含む COFF (Common Object File Format) ベースのイメージを使用して、Assembly を読み込みます。Loads the Assembly with a common object file format (COFF) based image containing an emitted Assembly.

Load(AssemblyName) Load(AssemblyName) Load(AssemblyName) Load(AssemblyName)

AssemblyName を指定して、Assembly を読み込みます。Loads an Assembly given its AssemblyName.

Load(String) Load(String) Load(String) Load(String)

表示名を指定して Assembly を読み込みます。Loads an Assembly given its display name.

Load(Byte[], Byte[]) Load(Byte[], Byte[]) Load(Byte[], Byte[]) Load(Byte[], Byte[])

生成された Assembly を含む COFF (Common Object File Format) ベースのイメージを使用して、Assembly を読み込みます。Loads the Assembly with a common object file format (COFF) based image containing an emitted Assembly. Assembly のシンボルを表す生バイトも読み込まれます。The raw bytes representing the symbols for the Assembly are also loaded.

Load(AssemblyName, Evidence) Load(AssemblyName, Evidence) Load(AssemblyName, Evidence) Load(AssemblyName, Evidence)

AssemblyName を指定して、Assembly を読み込みます。Loads an Assembly given its AssemblyName.

Load(String, Evidence) Load(String, Evidence) Load(String, Evidence) Load(String, Evidence)

表示名を指定して Assembly を読み込みます。Loads an Assembly given its display name.

Load(Byte[], Byte[], Evidence) Load(Byte[], Byte[], Evidence) Load(Byte[], Byte[], Evidence) Load(Byte[], Byte[], Evidence)

生成された Assembly を含む COFF (Common Object File Format) ベースのイメージを使用して、Assembly を読み込みます。Loads the Assembly with a common object file format (COFF) based image containing an emitted Assembly. Assembly のシンボルを表す生バイトも読み込まれます。The raw bytes representing the symbols for the Assembly are also loaded.

Load(Byte[]) Load(Byte[]) Load(Byte[]) Load(Byte[])

生成された Assembly を含む COFF (Common Object File Format) ベースのイメージを使用して、Assembly を読み込みます。Loads the Assembly with a common object file format (COFF) based image containing an emitted Assembly.

public:
 virtual System::Reflection::Assembly ^ Load(cli::array <System::Byte> ^ rawAssembly);
public System.Reflection.Assembly Load (byte[] rawAssembly);
abstract member Load : byte[] -> System.Reflection.Assembly
override this.Load : byte[] -> System.Reflection.Assembly
Public Function Load (rawAssembly As Byte()) As Assembly

パラメーター

rawAssembly
Byte[]

生成されたアセンブリを含む COFF ベースのイメージである byte 型配列。An array of type byte that is a COFF-based image containing an emitted assembly.

戻り値

読み込まれるアセンブリ。The loaded assembly.

実装

例外

rawAssembly は正しいアセンブリではありません。rawAssembly is not a valid assembly.

または-or- 共通言語ランタイムのバージョン 2.0 以降が現在読み込まれています。rawAssembly は、より新しいバージョンでコンパイルされています。Version 2.0 or later of the common language runtime is currently loaded and rawAssembly was compiled with a later version.

アンロードされたアプリケーション ドメインで操作しようとします。The operation is attempted on an unloaded application domain.

アセンブリまたはモジュールが、2 つの異なる証拠を使用して 2 回読み込まれました。An assembly or module was loaded twice with two different evidences.

次の例では、生のアセンブリの読み込みの使用を示します。The following sample demonstrates the use of loading a raw assembly.

このコード例を実行するには、完全修飾アセンブリ名を指定する必要があります。For this code example to run, you must provide the fully qualified assembly name. 完全修飾アセンブリ名を取得する方法については、次を参照してください。アセンブリ名します。For information about how to obtain the fully qualified assembly name, see Assembly Names.

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( "Assembly text name, Version, Culture, PublicKeyToken", "MyType" );
   }
   catch ( Exception^ e ) 
   {
      Console::WriteLine( e->Message );
   }

}


// Loads the content of a file to a Byte array.
array<Byte>^ loadFile( String^ filename )
{
   FileStream^ fs = gcnew FileStream( filename,FileMode::Open );
   array<Byte>^buffer = gcnew array<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 = gcnew 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, nullptr );
   ILGenerator^ ilGenerator = constructorBuilder->GetILGenerator();
   ilGenerator->EmitWriteLine( "MyType instantiated!" );
   ilGenerator->Emit( OpCodes::Ret );
   typeBuilder->CreateType();
   assemblyBuilder->Save( "temp.dll" );
}

ref 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 );
      array<Byte>^rawAssembly = loadFile( "temp.dll" );
      array<Byte>^rawSymbolStore = loadFile( "temp.pdb" );
      Assembly^ assembly = domain->Load( rawAssembly, rawSymbolStore );
      return assembly;
   }

};

int main()
{
   AppDomain^ currentDomain = AppDomain::CurrentDomain;
   InstantiateMyType( currentDomain ); // Failed!
   currentDomain->AssemblyResolve += gcnew ResolveEventHandler( Resolver::MyResolver );
   InstantiateMyType( currentDomain ); // OK!
}

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

注釈

以降では、 .NET Framework 4.NET Framework 4、このメソッドを使用して読み込まれるアセンブリの信頼レベルは、アプリケーション ドメインの信頼レベルと同じです。Beginning with the .NET Framework 4.NET Framework 4, the trust level of an assembly that is loaded by using this method is the same as the trust level of the application domain.

このメソッドは、現在のアプリケーション ドメインにアセンブリを読み込む場合にのみ使用する必要があります。This method should be used only to load an assembly into the current application domain. このメソッドが静的なを呼び出すことができない相互運用性の呼び出し元に、便宜上指定Assembly.Loadメソッド。This method is provided as a convenience for interoperability callers who cannot call the static Assembly.Load method. 他のアプリケーション ドメインにアセンブリを読み込む場合などメソッドを使用してCreateInstanceAndUnwrapします。To load assemblies into other application domains, use a method such as CreateInstanceAndUnwrap.

このメソッドのすべてのオーバー ロードする一般的な情報については、次を参照してください。、Load(AssemblyName)メソッドのオーバー ロードします。For information that is common to all overloads of this method, see the Load(AssemblyName) method overload.

セキュリティ

FileIOPermission
ファイルまたはディレクトリからと、パス自体の情報にアクセスするための読み取りにアクセスします。for access to read from a file or directory, and for access to the information in the path itself. 列挙体に関連付けられている: ReadPathDiscoveryします。Associated enumerations: Read, PathDiscovery.

Load(AssemblyName) Load(AssemblyName) Load(AssemblyName) Load(AssemblyName)

AssemblyName を指定して、Assembly を読み込みます。Loads an Assembly given its AssemblyName.

public:
 virtual System::Reflection::Assembly ^ Load(System::Reflection::AssemblyName ^ assemblyRef);
public System.Reflection.Assembly Load (System.Reflection.AssemblyName assemblyRef);
abstract member Load : System.Reflection.AssemblyName -> System.Reflection.Assembly
override this.Load : System.Reflection.AssemblyName -> System.Reflection.Assembly
Public Function Load (assemblyRef As AssemblyName) As Assembly

パラメーター

assemblyRef
AssemblyName AssemblyName AssemblyName AssemblyName

読み込むアセンブリについて記述しているオブジェクト。An object that describes the assembly to load.

戻り値

読み込まれるアセンブリ。The loaded assembly.

実装

例外

assemblyRef が見つかりません。assemblyRef is not found.

assemblyRef は正しいアセンブリではありません。assemblyRef is not a valid assembly.

または-or- 共通言語ランタイムのバージョン 2.0 以降が現在読み込まれています。assemblyRef は、より新しいバージョンでコンパイルされています。Version 2.0 or later of the common language runtime is currently loaded and assemblyRef was compiled with a later version.

アンロードされたアプリケーション ドメインで操作しようとします。The operation is attempted on an unloaded application domain.

アセンブリまたはモジュールが、2 つの異なる証拠を使用して 2 回読み込まれました。An assembly or module was loaded twice with two different evidences.

注釈

このメソッドは、現在のアプリケーション ドメインにアセンブリを読み込む場合にのみ使用する必要があります。This method should be used only to load an assembly into the current application domain. このメソッドが静的なを呼び出すことができない相互運用性の呼び出し元に、便宜上指定Assembly.Loadメソッド。This method is provided as a convenience for interoperability callers who cannot call the static Assembly.Load method. 他のアプリケーション ドメインにアセンブリを読み込む場合などメソッドを使用してCreateInstanceAndUnwrapします。To load assemblies into other application domains, use a method such as CreateInstanceAndUnwrap.

要求されたアセンブリのバージョンが既に読み込まれている場合、このメソッドは、別のバージョンが要求された場合でも、読み込まれたアセンブリを返します。If a version of the requested assembly is already loaded, this method returns the loaded assembly, even if a different version is requested.

部分的なアセンブリ名を指定してassemblyRefはお勧めしません。Supplying a partial assembly name for assemblyRef is not recommended. (部分的な名前は、1 つまたは複数のカルチャ、バージョン、または公開キー トークンを省略します。(A partial name omits one or more of culture, version, or public key token. 代わりに文字列を受け取るオーバー ロードの場合、AssemblyNameオブジェクト、"MyAssembly, バージョン 1.0.0.0 を ="部分的な名前の例を示しますと"MyAssembly, バージョン 1.0.0.0、カルチャを = = neutral, PublicKeyToken = 18ab3442da84b47"完全な名前の例を示します)。パフォーマンスに悪影響を及ぼすが部分的な名前を使用します。For overloads that take a string instead of an AssemblyName object, "MyAssembly, Version=1.0.0.0" is an example of a partial name and "MyAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=18ab3442da84b47" is an example of a full name.) Using partial names has a negative effect on performance. さらに、部分的なアセンブリ名はアセンブリを読み込むグローバル アセンブリ キャッシュからアプリケーション ベース ディレクトリ内のアセンブリの正確なコピーがある場合にのみ (BaseDirectoryまたはAppDomainSetup.ApplicationBase)。In addition, a partial assembly name can load an assembly from the global assembly cache only if there is an exact copy of the assembly in the application base directory (BaseDirectory or AppDomainSetup.ApplicationBase).

場合、現在AppDomainオブジェクトは、アプリケーション ドメインを表すA、およびLoadメソッドの呼び出し元のアプリケーション ドメインBアセンブリは両方のアプリケーション ドメインに読み込まれます。If the current AppDomain object represents application domain A, and the Load method is called from application domain B, the assembly is loaded into both application domains. たとえば、次のコードの読み込みMyAssembly新しいアプリケーション ドメインにChildDomainとコードが実行されるアプリケーション ドメインにもします。For example, the following code loads MyAssembly into the new application domain ChildDomain and also into the application domain where the code executes:

AppDomain^ ad = AppDomain::CreateDomain("ChildDomain");
ad->Load("MyAssembly");
AppDomain ad = AppDomain.CreateDomain("ChildDomain");
ad.Load("MyAssembly");
Dim ad As AppDomain  = AppDomain.CreateDomain("ChildDomain")
ad.Load("MyAssembly")

両方のドメインにアセンブリが読み込まれるAssemblyから派生していないMarshalByRefObject、および戻り値のため、Loadメソッドをマーシャ リングすることはできません。The assembly is loaded into both domains because Assembly does not derive from MarshalByRefObject, and therefore the return value of the Load method cannot be marshaled. 代わりに、共通言語ランタイムは、呼び出し元のアプリケーション ドメインにアセンブリの読み込みを試みます。Instead, the common language runtime tries to load the assembly into the calling application domain. 2 つのアプリケーション ドメインに読み込まれるアセンブリは、2 つのアプリケーション ドメインのパスの設定が異なる場合は異なる可能性があります。The assemblies that are loaded into the two application domains might be different if the path settings for the two application domains are different.

注意

両方の場合、AssemblyName.NameプロパティとAssemblyName.CodeBaseプロパティが設定されて、アセンブリを読み込むには、最初の試行は、表示名を使用 (によって返されるバージョン、カルチャ、およびなどを含む、Assembly.FullNameプロパティ)。If both the AssemblyName.Name property and the AssemblyName.CodeBase property are set, the first attempt to load the assembly uses the display name (including version, culture, and so on, as returned by the Assembly.FullName property). ファイルが見つからない場合、CodeBaseプロパティを使用して、アセンブリを検索します。If the file is not found, the CodeBase property is used to search for the assembly. 使用して、アセンブリが見つかった場合CodeBase表示名のアセンブリが照合されます。If the assembly is found using CodeBase, the display name is matched against the assembly. 一致が見つからない場合、FileLoadExceptionがスローされます。If the match fails, a FileLoadException is thrown.

セキュリティ

FileIOPermission
ファイルまたはディレクトリからと、パス自体の情報にアクセスするための読み取りにアクセスします。for access to read from a file or directory, and for access to the information in the path itself. 列挙体に関連付けられている: ReadPathDiscoveryします。Associated enumerations: Read, PathDiscovery.

Load(String) Load(String) Load(String) Load(String)

表示名を指定して Assembly を読み込みます。Loads an Assembly given its display name.

public:
 virtual System::Reflection::Assembly ^ Load(System::String ^ assemblyString);
public System.Reflection.Assembly Load (string assemblyString);
abstract member Load : string -> System.Reflection.Assembly
override this.Load : string -> System.Reflection.Assembly
Public Function Load (assemblyString As String) As Assembly

パラメーター

assemblyString
String String String String

アセンブリの表示名。The display name of the assembly. 以下を参照してください。FullNameSee FullName.

戻り値

読み込まれるアセンブリ。The loaded assembly.

実装

例外

assemblyStringnull ですassemblyString is null

assemblyString が見つかりません。assemblyString is not found.

assemblyString は正しいアセンブリではありません。assemblyString is not a valid assembly.

または-or- 共通言語ランタイムのバージョン 2.0 以降が現在読み込まれています。assemblyString は、より新しいバージョンでコンパイルされています。Version 2.0 or later of the common language runtime is currently loaded and assemblyString was compiled with a later version.

アンロードされたアプリケーション ドメインで操作しようとします。The operation is attempted on an unloaded application domain.

アセンブリまたはモジュールが、2 つの異なる証拠を使用して 2 回読み込まれました。An assembly or module was loaded twice with two different evidences.

注釈

このメソッドは、現在のアプリケーション ドメインにアセンブリを読み込む場合にのみ使用する必要があります。This method should be used only to load an assembly into the current application domain. このメソッドが静的なを呼び出すことができない相互運用性の呼び出し元に、便宜上指定Assembly.Loadメソッド。This method is provided as a convenience for interoperability callers who cannot call the static Assembly.Load method. 他のアプリケーション ドメインにアセンブリを読み込む場合などメソッドを使用してCreateInstanceAndUnwrapします。To load assemblies into other application domains, use a method such as CreateInstanceAndUnwrap.

このメソッドのすべてのオーバー ロードする一般的な情報については、次を参照してください。、Load(AssemblyName)メソッドのオーバー ロードします。For information that is common to all overloads of this method, see the Load(AssemblyName) method overload.

セキュリティ

FileIOPermission
ファイルまたはディレクトリからと、パス自体の情報にアクセスするための読み取りにアクセスします。for access to read from a file or directory, and for access to the information in the path itself. 列挙体に関連付けられている: ReadPathDiscoveryします。Associated enumerations: Read, PathDiscovery.

WebPermission
アセンブリがローカルでない場合、アセンブリの場所にアクセスする機能。for the ability to access the location of the assembly if the assembly is not local.

Load(Byte[], Byte[]) Load(Byte[], Byte[]) Load(Byte[], Byte[]) Load(Byte[], Byte[])

生成された Assembly を含む COFF (Common Object File Format) ベースのイメージを使用して、Assembly を読み込みます。Loads the Assembly with a common object file format (COFF) based image containing an emitted Assembly. Assembly のシンボルを表す生バイトも読み込まれます。The raw bytes representing the symbols for the Assembly are also loaded.

public:
 virtual System::Reflection::Assembly ^ Load(cli::array <System::Byte> ^ rawAssembly, cli::array <System::Byte> ^ rawSymbolStore);
public System.Reflection.Assembly Load (byte[] rawAssembly, byte[] rawSymbolStore);
abstract member Load : byte[] * byte[] -> System.Reflection.Assembly
override this.Load : byte[] * byte[] -> System.Reflection.Assembly
Public Function Load (rawAssembly As Byte(), rawSymbolStore As Byte()) As Assembly

パラメーター

rawAssembly
Byte[]

生成されたアセンブリを含む COFF ベースのイメージである byte 型配列。An array of type byte that is a COFF-based image containing an emitted assembly.

rawSymbolStore
Byte[]

アセンブリのシンボルを表す生バイトを格納している byte 型の配列。An array of type byte containing the raw bytes representing the symbols for the assembly.

戻り値

読み込まれるアセンブリ。The loaded assembly.

実装

例外

rawAssembly は正しいアセンブリではありません。rawAssembly is not a valid assembly.

または-or- 共通言語ランタイムのバージョン 2.0 以降が現在読み込まれています。rawAssembly は、より新しいバージョンでコンパイルされています。Version 2.0 or later of the common language runtime is currently loaded and rawAssembly was compiled with a later version.

アンロードされたアプリケーション ドメインで操作しようとします。The operation is attempted on an unloaded application domain.

アセンブリまたはモジュールが、2 つの異なる証拠を使用して 2 回読み込まれました。An assembly or module was loaded twice with two different evidences.

次の例では、生のアセンブリの読み込みの使用を示します。The following sample demonstrates the use of loading a raw assembly.

このコード例を実行するには、完全修飾アセンブリ名を指定する必要があります。For this code example to run, you must provide the fully qualified assembly name. 完全修飾アセンブリ名を取得する方法については、次を参照してください。アセンブリ名します。For information about how to obtain the fully qualified assembly name, see Assembly Names.

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( "Assembly text name, Version, Culture, PublicKeyToken", "MyType" );
   }
   catch ( Exception^ e ) 
   {
      Console::WriteLine( e->Message );
   }

}


// Loads the content of a file to a Byte array.
array<Byte>^ loadFile( String^ filename )
{
   FileStream^ fs = gcnew FileStream( filename,FileMode::Open );
   array<Byte>^buffer = gcnew array<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 = gcnew 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, nullptr );
   ILGenerator^ ilGenerator = constructorBuilder->GetILGenerator();
   ilGenerator->EmitWriteLine( "MyType instantiated!" );
   ilGenerator->Emit( OpCodes::Ret );
   typeBuilder->CreateType();
   assemblyBuilder->Save( "temp.dll" );
}

ref 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 );
      array<Byte>^rawAssembly = loadFile( "temp.dll" );
      array<Byte>^rawSymbolStore = loadFile( "temp.pdb" );
      Assembly^ assembly = domain->Load( rawAssembly, rawSymbolStore );
      return assembly;
   }

};

int main()
{
   AppDomain^ currentDomain = AppDomain::CurrentDomain;
   InstantiateMyType( currentDomain ); // Failed!
   currentDomain->AssemblyResolve += gcnew ResolveEventHandler( Resolver::MyResolver );
   InstantiateMyType( currentDomain ); // OK!
}

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

注釈

以降では、 .NET Framework 4.NET Framework 4、このメソッドを使用して読み込まれるアセンブリの信頼レベルは、アプリケーション ドメインの信頼レベルと同じです。Beginning with the .NET Framework 4.NET Framework 4, the trust level of an assembly that is loaded by using this method is the same as the trust level of the application domain.

このメソッドは、現在のアプリケーション ドメインにアセンブリを読み込む場合にのみ使用する必要があります。This method should be used only to load an assembly into the current application domain. このメソッドが静的なを呼び出すことができない相互運用性の呼び出し元に、便宜上指定Assembly.Loadメソッド。This method is provided as a convenience for interoperability callers who cannot call the static Assembly.Load method. 他のアプリケーション ドメインにアセンブリを読み込む場合などメソッドを使用してCreateInstanceAndUnwrapします。To load assemblies into other application domains, use a method such as CreateInstanceAndUnwrap.

このメソッドのすべてのオーバー ロードする一般的な情報については、次を参照してください。、Load(AssemblyName)メソッドのオーバー ロードします。For information that is common to all overloads of this method, see the Load(AssemblyName) method overload.

セキュリティ

WebPermission
file:// で始まらないする URI を読み取る。for reading a URI that does not begin with "file://".

FileIOPermission
ファイルまたはディレクトリからと、パス自体の情報にアクセスするための読み取りにアクセスします。for access to read from a file or directory, and for access to the information in the path itself. 列挙体に関連付けられている: ReadPathDiscoveryします。Associated enumerations: Read, PathDiscovery.

Load(AssemblyName, Evidence) Load(AssemblyName, Evidence) Load(AssemblyName, Evidence) Load(AssemblyName, Evidence)

警告

この API は現在使われていません。

AssemblyName を指定して、Assembly を読み込みます。Loads an Assembly given its AssemblyName.

public:
 virtual System::Reflection::Assembly ^ Load(System::Reflection::AssemblyName ^ assemblyRef, System::Security::Policy::Evidence ^ assemblySecurity);
[System.Obsolete("Use an overload that does not take an Evidence parameter")]
[System.Obsolete("Methods which use evidence to sandbox are obsolete and will be removed in a future release of the .NET Framework. Please use an overload of Load which does not take an Evidence parameter. See http://go.microsoft.com/fwlink/?LinkID=155570 for more information.")]
public System.Reflection.Assembly Load (System.Reflection.AssemblyName assemblyRef, System.Security.Policy.Evidence assemblySecurity);
abstract member Load : System.Reflection.AssemblyName * System.Security.Policy.Evidence -> System.Reflection.Assembly
override this.Load : System.Reflection.AssemblyName * System.Security.Policy.Evidence -> System.Reflection.Assembly
Public Function Load (assemblyRef As AssemblyName, assemblySecurity As Evidence) As Assembly

パラメーター

assemblyRef
AssemblyName AssemblyName AssemblyName AssemblyName

読み込むアセンブリについて記述しているオブジェクト。An object that describes the assembly to load.

assemblySecurity
Evidence Evidence Evidence Evidence

アセンブリを読み込むために必要な証拠。Evidence for loading the assembly.

戻り値

読み込まれるアセンブリ。The loaded assembly.

実装

例外

assemblyRef が見つかりません。assemblyRef is not found.

assemblyRef は正しいアセンブリではありません。assemblyRef is not a valid assembly.

または-or- 共通言語ランタイムのバージョン 2.0 以降が現在読み込まれています。assemblyRef は、より新しいバージョンでコンパイルされています。Version 2.0 or later of the common language runtime is currently loaded and assemblyRef was compiled with a later version.

アンロードされたアプリケーション ドメインで操作しようとします。The operation is attempted on an unloaded application domain.

アセンブリまたはモジュールが、2 つの異なる証拠を使用して 2 回読み込まれました。An assembly or module was loaded twice with two different evidences.

注釈

このメソッドは、現在のアプリケーション ドメインにアセンブリを読み込む場合にのみ使用する必要があります。This method should be used only to load an assembly into the current application domain. このメソッドが静的なを呼び出すことができない相互運用性の呼び出し元に、便宜上指定Assembly.Loadメソッド。This method is provided as a convenience for interoperability callers who cannot call the static Assembly.Load method. 他のアプリケーション ドメインにアセンブリを読み込む場合などメソッドを使用してCreateInstanceAndUnwrapします。To load assemblies into other application domains, use a method such as CreateInstanceAndUnwrap.

このメソッドのすべてのオーバー ロードする一般的な情報については、次を参照してください。、Load(AssemblyName)メソッドのオーバー ロードします。For information that is common to all overloads of this method, see the Load(AssemblyName) method overload.

セキュリティ

FileIOPermission
ファイルまたはディレクトリからと、パス自体の情報にアクセスするための読み取りにアクセスします。for access to read from a file or directory, and for access to the information in the path itself. 列挙体に関連付けられている: ReadPathDiscoveryします。Associated enumerations: Read, PathDiscovery.

SecurityPermission
証拠とアセンブリの読み込み。to load an assembly with evidence. ControlEvidence (関連する列挙体)Associated enumeration: ControlEvidence.

WebPermission
file:// フォームではないパスを読み取るため、または"\\\UNC\dir\\"または"c:\\"。for reading a path that is not in the form "file://" or "\\\UNC\dir\\" or "c:\\".

Load(String, Evidence) Load(String, Evidence) Load(String, Evidence) Load(String, Evidence)

警告

この API は現在使われていません。

表示名を指定して Assembly を読み込みます。Loads an Assembly given its display name.

public:
 virtual System::Reflection::Assembly ^ Load(System::String ^ assemblyString, System::Security::Policy::Evidence ^ assemblySecurity);
[System.Obsolete("Use an overload that does not take an Evidence parameter")]
[System.Obsolete("Methods which use evidence to sandbox are obsolete and will be removed in a future release of the .NET Framework. Please use an overload of Load which does not take an Evidence parameter. See http://go.microsoft.com/fwlink/?LinkID=155570 for more information.")]
public System.Reflection.Assembly Load (string assemblyString, System.Security.Policy.Evidence assemblySecurity);
abstract member Load : string * System.Security.Policy.Evidence -> System.Reflection.Assembly
override this.Load : string * System.Security.Policy.Evidence -> System.Reflection.Assembly
Public Function Load (assemblyString As String, assemblySecurity As Evidence) As Assembly

パラメーター

assemblyString
String String String String

アセンブリの表示名。The display name of the assembly. 以下を参照してください。FullNameSee FullName.

assemblySecurity
Evidence Evidence Evidence Evidence

アセンブリを読み込むために必要な証拠。Evidence for loading the assembly.

戻り値

読み込まれるアセンブリ。The loaded assembly.

実装

例外

assemblyStringnull ですassemblyString is null

assemblyString が見つかりません。assemblyString is not found.

assemblyString は正しいアセンブリではありません。assemblyString is not a valid assembly.

または-or- 共通言語ランタイムのバージョン 2.0 以降が現在読み込まれています。assemblyString は、より新しいバージョンでコンパイルされています。Version 2.0 or later of the common language runtime is currently loaded and assemblyString was compiled with a later version.

アンロードされたアプリケーション ドメインで操作しようとします。The operation is attempted on an unloaded application domain.

アセンブリまたはモジュールが、2 つの異なる証拠を使用して 2 回読み込まれました。An assembly or module was loaded twice with two different evidences.

注釈

このメソッドは、現在のアプリケーション ドメインにアセンブリを読み込む場合にのみ使用する必要があります。This method should be used only to load an assembly into the current application domain. このメソッドが静的なを呼び出すことができない相互運用性の呼び出し元に、便宜上指定Assembly.Loadメソッド。This method is provided as a convenience for interoperability callers who cannot call the static Assembly.Load method. 他のアプリケーション ドメインにアセンブリを読み込む場合などメソッドを使用してCreateInstanceAndUnwrapします。To load assemblies into other application domains, use a method such as CreateInstanceAndUnwrap.

このメソッドのすべてのオーバー ロードする一般的な情報については、次を参照してください。、Load(AssemblyName)メソッドのオーバー ロードします。For information that is common to all overloads of this method, see the Load(AssemblyName) method overload.

セキュリティ

SecurityPermission
証拠とアセンブリの読み込み。to load an assembly with evidence. ControlEvidence (関連する列挙体)Associated enumeration: ControlEvidence.

FileIOPermission
ファイルまたはディレクトリからと、パス自体の情報にアクセスするための読み取りにアクセスします。for access to read from a file or directory, and for access to the information in the path itself. 列挙体に関連付けられている: ReadPathDiscoveryします。Associated enumerations: Read, PathDiscovery.

WebPermission
file:// フォームではないパスを読み取るため、または"\\\UNC\dir\\"または"c:\\"。for reading a path that is not in the form "file://" or "\\\UNC\dir\\" or "c:\\".

Load(Byte[], Byte[], Evidence) Load(Byte[], Byte[], Evidence) Load(Byte[], Byte[], Evidence) Load(Byte[], Byte[], Evidence)

警告

この API は現在使われていません。

生成された Assembly を含む COFF (Common Object File Format) ベースのイメージを使用して、Assembly を読み込みます。Loads the Assembly with a common object file format (COFF) based image containing an emitted Assembly. Assembly のシンボルを表す生バイトも読み込まれます。The raw bytes representing the symbols for the Assembly are also loaded.

public:
 virtual System::Reflection::Assembly ^ Load(cli::array <System::Byte> ^ rawAssembly, cli::array <System::Byte> ^ rawSymbolStore, System::Security::Policy::Evidence ^ securityEvidence);
[System.Obsolete("Use an overload that does not take an Evidence parameter")]
[System.Obsolete("Methods which use evidence to sandbox are obsolete and will be removed in a future release of the .NET Framework. Please use an overload of Load which does not take an Evidence parameter. See http://go.microsoft.com/fwlink/?LinkId=155570 for more information.")]
public System.Reflection.Assembly Load (byte[] rawAssembly, byte[] rawSymbolStore, System.Security.Policy.Evidence securityEvidence);
abstract member Load : byte[] * byte[] * System.Security.Policy.Evidence -> System.Reflection.Assembly
override this.Load : byte[] * byte[] * System.Security.Policy.Evidence -> System.Reflection.Assembly
Public Function Load (rawAssembly As Byte(), rawSymbolStore As Byte(), securityEvidence As Evidence) As Assembly

パラメーター

rawAssembly
Byte[]

生成されたアセンブリを含む COFF ベースのイメージである byte 型配列。An array of type byte that is a COFF-based image containing an emitted assembly.

rawSymbolStore
Byte[]

アセンブリのシンボルを表す生バイトを格納している byte 型の配列。An array of type byte containing the raw bytes representing the symbols for the assembly.

securityEvidence
Evidence Evidence Evidence Evidence

アセンブリを読み込むために必要な証拠。Evidence for loading the assembly.

戻り値

読み込まれるアセンブリ。The loaded assembly.

実装

例外

rawAssembly は正しいアセンブリではありません。rawAssembly is not a valid assembly.

または-or- 共通言語ランタイムのバージョン 2.0 以降が現在読み込まれています。rawAssembly は、より新しいバージョンでコンパイルされています。Version 2.0 or later of the common language runtime is currently loaded and rawAssembly was compiled with a later version.

アンロードされたアプリケーション ドメインで操作しようとします。The operation is attempted on an unloaded application domain.

アセンブリまたはモジュールが、2 つの異なる証拠を使用して 2 回読み込まれました。An assembly or module was loaded twice with two different evidences.

securityEvidencenull ではありません。securityEvidence is not null. レガシ CAS ポリシーが有効でない場合は、securityEvidencenull にする必要があります。When legacy CAS policy is not enabled, securityEvidence should be null.

次の例では、生のアセンブリの読み込みの使用を示します。The following sample demonstrates the use of loading a raw assembly.

このコード例を実行するには、完全修飾アセンブリ名を指定する必要があります。For this code example to run, you must provide the fully qualified assembly name. 完全修飾アセンブリ名を取得する方法については、次を参照してください。アセンブリ名します。For information about how to obtain the fully qualified assembly name, see Assembly Names.

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( "Assembly text name, Version, Culture, PublicKeyToken", "MyType" );
   }
   catch ( Exception^ e ) 
   {
      Console::WriteLine( e->Message );
   }

}


// Loads the content of a file to a Byte array.
array<Byte>^ loadFile( String^ filename )
{
   FileStream^ fs = gcnew FileStream( filename,FileMode::Open );
   array<Byte>^buffer = gcnew array<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 = gcnew 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, nullptr );
   ILGenerator^ ilGenerator = constructorBuilder->GetILGenerator();
   ilGenerator->EmitWriteLine( "MyType instantiated!" );
   ilGenerator->Emit( OpCodes::Ret );
   typeBuilder->CreateType();
   assemblyBuilder->Save( "temp.dll" );
}

ref 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 );
      array<Byte>^rawAssembly = loadFile( "temp.dll" );
      array<Byte>^rawSymbolStore = loadFile( "temp.pdb" );
      Assembly^ assembly = domain->Load( rawAssembly, rawSymbolStore );
      return assembly;
   }

};

int main()
{
   AppDomain^ currentDomain = AppDomain::CurrentDomain;
   InstantiateMyType( currentDomain ); // Failed!
   currentDomain->AssemblyResolve += gcnew ResolveEventHandler( Resolver::MyResolver );
   InstantiateMyType( currentDomain ); // OK!
}

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

注釈

以降では、 .NET Framework 4.NET Framework 4、このメソッドを使用して読み込まれるアセンブリの信頼レベルは、アプリケーション ドメインの信頼レベルと同じです。Beginning with the .NET Framework 4.NET Framework 4, the trust level of an assembly that is loaded by using this method is the same as the trust level of the application domain.

このメソッドは、現在のアプリケーション ドメインにアセンブリを読み込む場合にのみ使用する必要があります。This method should be used only to load an assembly into the current application domain. このメソッドが静的なを呼び出すことができない相互運用性の呼び出し元に、便宜上指定Assembly.Loadメソッド。This method is provided as a convenience for interoperability callers who cannot call the static Assembly.Load method. 他のアプリケーション ドメインにアセンブリを読み込む場合などメソッドを使用してCreateInstanceAndUnwrapします。To load assemblies into other application domains, use a method such as CreateInstanceAndUnwrap.

このメソッドのすべてのオーバー ロードする一般的な情報については、次を参照してください。、Load(AssemblyName)メソッドのオーバー ロードします。For information that is common to all overloads of this method, see the Load(AssemblyName) method overload.

セキュリティ

SecurityPermission
証拠を提供します。to provide evidence. ControlEvidence (関連する列挙体)Associated enumeration: ControlEvidence. セキュリティ アクション:Demandします。Security action: Demand.

FileIOPermission
ファイルまたはディレクトリからと、パス自体の情報にアクセスするための読み取りにアクセスします。for access to read from a file or directory, and for access to the information in the path itself. 列挙体に関連付けられている: ReadPathDiscoveryします。Associated enumerations: Read, PathDiscovery.

WebPermission
file:// フォームではないパスを読み取るため、または"\\\UNC\dir\\"または"c:\\"。for reading a path that is not in the form "file://" or "\\\UNC\dir\\" or "c:\\".

適用対象