AppDomain.DefineDynamicAssembly AppDomain.DefineDynamicAssembly AppDomain.DefineDynamicAssembly AppDomain.DefineDynamicAssembly Method

定義

現在のアプリケーション ドメインで動的アセンブリを定義します。Defines a dynamic assembly in the current application domain.

オーバーロード

DefineDynamicAssembly(AssemblyName, AssemblyBuilderAccess, String, Evidence, PermissionSet, PermissionSet, PermissionSet, Boolean, IEnumerable<CustomAttributeBuilder>) DefineDynamicAssembly(AssemblyName, AssemblyBuilderAccess, String, Evidence, PermissionSet, PermissionSet, PermissionSet, Boolean, IEnumerable<CustomAttributeBuilder>) DefineDynamicAssembly(AssemblyName, AssemblyBuilderAccess, String, Evidence, PermissionSet, PermissionSet, PermissionSet, Boolean, IEnumerable<CustomAttributeBuilder>)

名前、アクセス モード、ストレージ ディレクトリ、証拠、アクセス許可要求、同期オプション、およびカスタム属性を指定して、動的アセンブリを定義します。Defines a dynamic assembly with the specified name, access mode, storage directory, evidence, permission requests, synchronization option, and custom attributes.

DefineDynamicAssembly(AssemblyName, AssemblyBuilderAccess, String, Evidence, PermissionSet, PermissionSet, PermissionSet, Boolean) DefineDynamicAssembly(AssemblyName, AssemblyBuilderAccess, String, Evidence, PermissionSet, PermissionSet, PermissionSet, Boolean) DefineDynamicAssembly(AssemblyName, AssemblyBuilderAccess, String, Evidence, PermissionSet, PermissionSet, PermissionSet, Boolean)

名前、アクセス モード、ストレージ ディレクトリ、証拠、アクセス許可要求、および同期オプションを指定して、動的アセンブリを定義します。Defines a dynamic assembly using the specified name, access mode, storage directory, evidence, permission requests, and synchronization option.

DefineDynamicAssembly(AssemblyName, AssemblyBuilderAccess, String, Evidence, PermissionSet, PermissionSet, PermissionSet) DefineDynamicAssembly(AssemblyName, AssemblyBuilderAccess, String, Evidence, PermissionSet, PermissionSet, PermissionSet) DefineDynamicAssembly(AssemblyName, AssemblyBuilderAccess, String, Evidence, PermissionSet, PermissionSet, PermissionSet)

名前、アクセス モード、ストレージ ディレクトリ、証拠、およびアクセス許可要求を指定して、動的アセンブリを定義します。Defines a dynamic assembly using the specified name, access mode, storage directory, evidence, and permission requests.

DefineDynamicAssembly(AssemblyName, AssemblyBuilderAccess, String, PermissionSet, PermissionSet, PermissionSet) DefineDynamicAssembly(AssemblyName, AssemblyBuilderAccess, String, PermissionSet, PermissionSet, PermissionSet) DefineDynamicAssembly(AssemblyName, AssemblyBuilderAccess, String, PermissionSet, PermissionSet, PermissionSet) DefineDynamicAssembly(AssemblyName, AssemblyBuilderAccess, String, PermissionSet, PermissionSet, PermissionSet)

名前、アクセス モード、ストレージ ディレクトリ、およびアクセス許可要求を指定して、動的アセンブリを定義します。Defines a dynamic assembly using the specified name, access mode, storage directory, and permission requests.

DefineDynamicAssembly(AssemblyName, AssemblyBuilderAccess, Evidence, PermissionSet, PermissionSet, PermissionSet) DefineDynamicAssembly(AssemblyName, AssemblyBuilderAccess, Evidence, PermissionSet, PermissionSet, PermissionSet) DefineDynamicAssembly(AssemblyName, AssemblyBuilderAccess, Evidence, PermissionSet, PermissionSet, PermissionSet)

名前、アクセス モード、証拠、およびアクセス許可要求を指定して、動的アセンブリを定義します。Defines a dynamic assembly using the specified name, access mode, evidence, and permission requests.

DefineDynamicAssembly(AssemblyName, AssemblyBuilderAccess, String, Boolean, IEnumerable<CustomAttributeBuilder>) DefineDynamicAssembly(AssemblyName, AssemblyBuilderAccess, String, Boolean, IEnumerable<CustomAttributeBuilder>) DefineDynamicAssembly(AssemblyName, AssemblyBuilderAccess, String, Boolean, IEnumerable<CustomAttributeBuilder>) DefineDynamicAssembly(AssemblyName, AssemblyBuilderAccess, String, Boolean, IEnumerable<CustomAttributeBuilder>)

名前、アクセス モード、ストレージ ディレクトリ、および同期オプションを指定して、動的アセンブリを定義します。Defines a dynamic assembly using the specified name, access mode, storage directory, and synchronization option.

DefineDynamicAssembly(AssemblyName, AssemblyBuilderAccess, PermissionSet, PermissionSet, PermissionSet) DefineDynamicAssembly(AssemblyName, AssemblyBuilderAccess, PermissionSet, PermissionSet, PermissionSet) DefineDynamicAssembly(AssemblyName, AssemblyBuilderAccess, PermissionSet, PermissionSet, PermissionSet) DefineDynamicAssembly(AssemblyName, AssemblyBuilderAccess, PermissionSet, PermissionSet, PermissionSet)

名前、アクセス モード、およびアクセス許可要求を指定して、動的アセンブリを定義します。Defines a dynamic assembly using the specified name, access mode, and permission requests.

DefineDynamicAssembly(AssemblyName, AssemblyBuilderAccess, String, Evidence) DefineDynamicAssembly(AssemblyName, AssemblyBuilderAccess, String, Evidence) DefineDynamicAssembly(AssemblyName, AssemblyBuilderAccess, String, Evidence)

名前、アクセス モード、ストレージ ディレクトリ、および証拠を指定して、動的アセンブリを定義します。Defines a dynamic assembly using the specified name, access mode, storage directory, and evidence.

DefineDynamicAssembly(AssemblyName, AssemblyBuilderAccess, IEnumerable<CustomAttributeBuilder>, SecurityContextSource) DefineDynamicAssembly(AssemblyName, AssemblyBuilderAccess, IEnumerable<CustomAttributeBuilder>, SecurityContextSource) DefineDynamicAssembly(AssemblyName, AssemblyBuilderAccess, IEnumerable<CustomAttributeBuilder>, SecurityContextSource)

名前、アクセス モード、カスタム属性、およびセキュリティ コンテキストのソースを指定して、動的アセンブリを定義します。Defines a dynamic assembly with the specified name, access mode, and custom attributes, and using the specified source for its security context.

DefineDynamicAssembly(AssemblyName, AssemblyBuilderAccess, String) DefineDynamicAssembly(AssemblyName, AssemblyBuilderAccess, String) DefineDynamicAssembly(AssemblyName, AssemblyBuilderAccess, String) DefineDynamicAssembly(AssemblyName, AssemblyBuilderAccess, String)

名前、アクセス モード、およびストレージ ディレクトリを指定して、動的アセンブリを定義します。Defines a dynamic assembly using the specified name, access mode, and storage directory.

DefineDynamicAssembly(AssemblyName, AssemblyBuilderAccess, Evidence) DefineDynamicAssembly(AssemblyName, AssemblyBuilderAccess, Evidence) DefineDynamicAssembly(AssemblyName, AssemblyBuilderAccess, Evidence)

名前、アクセス モード、および証拠を指定して、動的アセンブリを定義します。Defines a dynamic assembly using the specified name, access mode, and evidence.

DefineDynamicAssembly(AssemblyName, AssemblyBuilderAccess, IEnumerable<CustomAttributeBuilder>) DefineDynamicAssembly(AssemblyName, AssemblyBuilderAccess, IEnumerable<CustomAttributeBuilder>) DefineDynamicAssembly(AssemblyName, AssemblyBuilderAccess, IEnumerable<CustomAttributeBuilder>) DefineDynamicAssembly(AssemblyName, AssemblyBuilderAccess, IEnumerable<CustomAttributeBuilder>)

名前、アクセス モード、およびカスタム属性を指定して、動的アセンブリを定義します。Defines a dynamic assembly with the specified name, access mode, and custom attributes.

DefineDynamicAssembly(AssemblyName, AssemblyBuilderAccess) DefineDynamicAssembly(AssemblyName, AssemblyBuilderAccess) DefineDynamicAssembly(AssemblyName, AssemblyBuilderAccess) DefineDynamicAssembly(AssemblyName, AssemblyBuilderAccess)

名前とアクセス モードを指定して、動的アセンブリを定義します。Defines a dynamic assembly with the specified name and access mode.

DefineDynamicAssembly(AssemblyName, AssemblyBuilderAccess, String, Evidence, PermissionSet, PermissionSet, PermissionSet, Boolean, IEnumerable<CustomAttributeBuilder>) DefineDynamicAssembly(AssemblyName, AssemblyBuilderAccess, String, Evidence, PermissionSet, PermissionSet, PermissionSet, Boolean, IEnumerable<CustomAttributeBuilder>) DefineDynamicAssembly(AssemblyName, AssemblyBuilderAccess, String, Evidence, PermissionSet, PermissionSet, PermissionSet, Boolean, IEnumerable<CustomAttributeBuilder>)

警告

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

名前、アクセス モード、ストレージ ディレクトリ、証拠、アクセス許可要求、同期オプション、およびカスタム属性を指定して、動的アセンブリを定義します。Defines a dynamic assembly with the specified name, access mode, storage directory, evidence, permission requests, synchronization option, and custom attributes.

public:
 System::Reflection::Emit::AssemblyBuilder ^ DefineDynamicAssembly(System::Reflection::AssemblyName ^ name, System::Reflection::Emit::AssemblyBuilderAccess access, System::String ^ dir, System::Security::Policy::Evidence ^ evidence, System::Security::PermissionSet ^ requiredPermissions, System::Security::PermissionSet ^ optionalPermissions, System::Security::PermissionSet ^ refusedPermissions, bool isSynchronized, System::Collections::Generic::IEnumerable<System::Reflection::Emit::CustomAttributeBuilder ^> ^ assemblyAttributes);
[System.Obsolete("Declarative security for assembly level is no longer enforced")]
[System.Obsolete("Assembly level declarative security is obsolete and is no longer enforced by the CLR by default. See http://go.microsoft.com/fwlink/?LinkID=155570 for more information.")]
public System.Reflection.Emit.AssemblyBuilder DefineDynamicAssembly (System.Reflection.AssemblyName name, System.Reflection.Emit.AssemblyBuilderAccess access, string dir, System.Security.Policy.Evidence evidence, System.Security.PermissionSet requiredPermissions, System.Security.PermissionSet optionalPermissions, System.Security.PermissionSet refusedPermissions, bool isSynchronized, System.Collections.Generic.IEnumerable<System.Reflection.Emit.CustomAttributeBuilder> assemblyAttributes);
member this.DefineDynamicAssembly : System.Reflection.AssemblyName * System.Reflection.Emit.AssemblyBuilderAccess * string * System.Security.Policy.Evidence * System.Security.PermissionSet * System.Security.PermissionSet * System.Security.PermissionSet * bool * seq<System.Reflection.Emit.CustomAttributeBuilder> -> System.Reflection.Emit.AssemblyBuilder

パラメーター

name
AssemblyName AssemblyName AssemblyName AssemblyName

動的アセンブリの一意の ID。The unique identity of the dynamic assembly.

access
AssemblyBuilderAccess AssemblyBuilderAccess AssemblyBuilderAccess AssemblyBuilderAccess

動的アセンブリにアクセスするときのモード。The mode in which the dynamic assembly will be accessed.

dir
String String String String

動的アセンブリを保存するディレクトリの名前。The name of the directory where the dynamic assembly will be saved. dirnull の場合、現在のディレクトリが使用されます。If dir is null, the current directory is used.

evidence
Evidence Evidence Evidence Evidence

動的アセンブリに指定する証拠。The evidence that is supplied for the dynamic assembly. 証拠は、ポリシーの解決に使用する証拠の最終セットを変更されないために使用されます。The evidence is used unaltered as the final set of evidence used for policy resolution.

requiredPermissions
PermissionSet PermissionSet PermissionSet PermissionSet

必須のアクセス許可要求。The required permissions request.

optionalPermissions
PermissionSet PermissionSet PermissionSet PermissionSet

オプションのアクセス許可要求。The optional permissions request.

refusedPermissions
PermissionSet PermissionSet PermissionSet PermissionSet

拒否されたアクセス許可要求。The refused permissions request.

isSynchronized
Boolean Boolean Boolean Boolean

動的アセンブリ内のモジュール、型、およびメンバーの作成を同期する場合は true。それ以外の場合は falsetrue to synchronize the creation of modules, types, and members in the dynamic assembly; otherwise, false.

assemblyAttributes
IEnumerable<CustomAttributeBuilder>

アセンブリに適用される属性を列挙した一覧。属性がない場合は nullAn enumerable list of attributes to be applied to the assembly, or null if there are no attributes.

戻り値

名前と機能が指定された動的アセンブリ。A dynamic assembly with the specified name and features.

例外

nameName プロパティが null です。The Name property of name is null.

または-or- nameName プロパティが空白で始まっているか、スラッシュまたは円記号が含まれています。The Name property of name starts with white space, or contains a forward or backward slash.

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

注釈

このメソッドのオーバー ロードを使用すると、動的アセンブリの作成時に適用しない限り、正しく動作していない属性を指定できます。Use this method overload to specify attributes that do not work correctly unless they are applied when a dynamic assembly is created. たとえば、セキュリティなどの属性SecurityTransparentAttributeSecurityCriticalAttribute動的アセンブリが作成された後に追加されている場合に正常に機能しません。For example, security attributes such as SecurityTransparentAttribute and SecurityCriticalAttribute do not work correctly if they are added after a dynamic assembly has been created.

指定されたアクセス許可要求、 requiredPermissionsoptionalPermissions、およびrefusedPermissions場合にのみ、パラメーターが使用される、evidenceパラメーターが指定されても、または動的アセンブリが保存され、メモリに再度読み込まれる場合。The permission requests specified for the requiredPermissions, optionalPermissions, and refusedPermissions parameters are used only if the evidence parameter is also supplied, or if the dynamic assembly is saved and reloaded into memory.

注意

含めることをお勧め動的アセンブリを生成するコードを開発する際に、SecurityPermissionFlag.SkipVerificationフラグ、refusedPermissionsパラメーター。When you develop code that emits dynamic assemblies, we recommend that you include the SecurityPermissionFlag.SkipVerification flag in the refusedPermissions parameter. このフラグを含めることにより、Microsoft intermediate language (MSIL) が検証されます。The inclusion of this flag ensures that the Microsoft intermediate language (MSIL) will be verified. この手法では、意図せずに検出するために非常に困難ですが、検証できないコードの生成を検出します。This technique will detect the unintentional generation of unverifiable code, which otherwise is very difficult to detect. この技法の制限はまた、あるSecurityException完全な信頼を要求するコードで使用した場合にスローされます。A limitation of this technique is that it also causes SecurityException to be thrown when it is used with code that demands full trust.

動的な定義するときに、完全に信頼された呼び出し元は証拠を提供できますのみAssemblyします。Only fully trusted callers can supply evidence when defining a dynamic Assembly. ランタイムのマップ、Evidence付与されたアクセス許可を決定するセキュリティ ポリシーを使用します。The runtime maps the Evidence through the security policy to determine the granted permissions. 呼び出し元を指定する必要がありますを部分的に信頼されたnullevidenceパラメーター。Partially trusted callers must supply null for the evidence parameter. 場合evidencenull、ランタイムは、定義されていると、ポリシーを解決済みとしてマークされる動的アセンブリを呼び出し元のアセンブリから (つまりは、現在の許可、セットを拒否) アクセス許可セットをコピーします。If evidence is null, the runtime copies the permission sets (that is, the current grant and deny sets) from the caller's assembly to the dynamic assembly that is being defined, and marks the policy as resolved.

動的アセンブリがディスク、その後に保存されているかどうかの負荷が動的アセンブリが保存された場所に関連付けられているポリシーに基づいて付与を取得します。If the dynamic assembly is saved to disk, subsequent loads will get grants based on policies that are associated with the location where the dynamic assembly was saved.

場合isSynchronizedtrue、結果の次のメソッドAssemblyBuilderを同期する: DefineDynamicModuleDefineResourceAddResourceFileGetDynamicModuleSetEntryPoint、およびSaveします。If isSynchronized is true, the following methods of the resulting AssemblyBuilder will be synchronized: DefineDynamicModule, DefineResource, AddResourceFile, GetDynamicModule, SetEntryPoint, and Save. これらのメソッドの 2 つは、異なるスレッドで呼び出されると、もう一方が完了するまで 1 つブロックされます。If two of these methods are called on different threads, one will block until the other is completed.

このメソッドのオーバー ロードがで導入された、 .NET Framework 3.5.NET Framework 3.5します。This method overload is introduced in the .NET Framework 3.5.NET Framework 3.5.

セキュリティ

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

こちらもご覧ください

DefineDynamicAssembly(AssemblyName, AssemblyBuilderAccess, String, Evidence, PermissionSet, PermissionSet, PermissionSet, Boolean) DefineDynamicAssembly(AssemblyName, AssemblyBuilderAccess, String, Evidence, PermissionSet, PermissionSet, PermissionSet, Boolean) DefineDynamicAssembly(AssemblyName, AssemblyBuilderAccess, String, Evidence, PermissionSet, PermissionSet, PermissionSet, Boolean)

警告

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

名前、アクセス モード、ストレージ ディレクトリ、証拠、アクセス許可要求、および同期オプションを指定して、動的アセンブリを定義します。Defines a dynamic assembly using the specified name, access mode, storage directory, evidence, permission requests, and synchronization option.

public:
 virtual System::Reflection::Emit::AssemblyBuilder ^ DefineDynamicAssembly(System::Reflection::AssemblyName ^ name, System::Reflection::Emit::AssemblyBuilderAccess access, System::String ^ dir, System::Security::Policy::Evidence ^ evidence, System::Security::PermissionSet ^ requiredPermissions, System::Security::PermissionSet ^ optionalPermissions, System::Security::PermissionSet ^ refusedPermissions, bool isSynchronized);
[System.Obsolete("Declarative security for assembly level is no longer enforced")]
[System.Obsolete("Assembly level declarative security is obsolete and is no longer enforced by the CLR by default. See http://go.microsoft.com/fwlink/?LinkID=155570 for more information.")]
public System.Reflection.Emit.AssemblyBuilder DefineDynamicAssembly (System.Reflection.AssemblyName name, System.Reflection.Emit.AssemblyBuilderAccess access, string dir, System.Security.Policy.Evidence evidence, System.Security.PermissionSet requiredPermissions, System.Security.PermissionSet optionalPermissions, System.Security.PermissionSet refusedPermissions, bool isSynchronized);
abstract member DefineDynamicAssembly : System.Reflection.AssemblyName * System.Reflection.Emit.AssemblyBuilderAccess * string * System.Security.Policy.Evidence * System.Security.PermissionSet * System.Security.PermissionSet * System.Security.PermissionSet * bool -> System.Reflection.Emit.AssemblyBuilder
override this.DefineDynamicAssembly : System.Reflection.AssemblyName * System.Reflection.Emit.AssemblyBuilderAccess * string * System.Security.Policy.Evidence * System.Security.PermissionSet * System.Security.PermissionSet * System.Security.PermissionSet * bool -> System.Reflection.Emit.AssemblyBuilder

パラメーター

name
AssemblyName AssemblyName AssemblyName AssemblyName

動的アセンブリの一意の ID。The unique identity of the dynamic assembly.

access
AssemblyBuilderAccess AssemblyBuilderAccess AssemblyBuilderAccess AssemblyBuilderAccess

動的アセンブリにアクセスするときのモード。The mode in which the dynamic assembly will be accessed.

dir
String String String String

動的アセンブリを保存するディレクトリの名前。The name of the directory where the dynamic assembly will be saved. dirnull の場合は、現在のディレクトリが既定で使用されます。If dir is null, the directory defaults to the current directory.

evidence
Evidence Evidence Evidence Evidence

動的アセンブリに指定する証拠。The evidence supplied for the dynamic assembly. 証拠は、ポリシーの解決に使用する証拠の最終セットを変更されないために使用されます。The evidence is used unaltered as the final set of evidence used for policy resolution.

requiredPermissions
PermissionSet PermissionSet PermissionSet PermissionSet

必須のアクセス許可要求。The required permissions request.

optionalPermissions
PermissionSet PermissionSet PermissionSet PermissionSet

オプションのアクセス許可要求。The optional permissions request.

refusedPermissions
PermissionSet PermissionSet PermissionSet PermissionSet

拒否されたアクセス許可要求。The refused permissions request.

isSynchronized
Boolean Boolean Boolean Boolean

動的アセンブリ内のモジュール、型、およびメンバーの作成を同期する場合は true。それ以外の場合は falsetrue to synchronize the creation of modules, types, and members in the dynamic assembly; otherwise, false.

戻り値

名前と機能が指定された動的アセンブリ。A dynamic assembly with the specified name and features.

実装

例外

nameName プロパティが null です。The Name property of name is null.

または-or- nameName プロパティが空白文字で始まるか、スラッシュまたはバック スラッシュを含んでいます。The Name property of name begins with white space, or contains a forward or backward slash.

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

次のサンプルでは、DefineDynamicAssemblyメソッドとAssemblyResolveイベント。The following sample demonstrates the DefineDynamicAssembly method and AssemblyResolve event.

このコード例を実行するには、完全修飾アセンブリ名を指定する必要があります。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::Reflection;
using namespace System::Reflection::Emit;
ref class Test
{
public:
   static void InstantiateMyDynamicType( AppDomain^ domain )
   {
      try
      {
         
         // You must supply a valid fully qualified assembly name here.
         domain->CreateInstance( "Assembly text name, Version, Culture, PublicKeyToken", "MyDynamicType" );
      }
      catch ( Exception^ e ) 
      {
         Console::WriteLine( e->Message );
      }

   }

   static Assembly^ MyResolveEventHandler( Object^ sender, ResolveEventArgs^ args )
   {
      return DefineDynamicAssembly( dynamic_cast<AppDomain^>(sender) );
   }

   static Assembly^ DefineDynamicAssembly( AppDomain^ domain )
   {
      
      // Build a dynamic assembly using Reflection Emit API.
      AssemblyName^ assemblyName = gcnew AssemblyName;
      assemblyName->Name = "MyDynamicAssembly";
      AssemblyBuilder^ assemblyBuilder = domain->DefineDynamicAssembly( assemblyName, AssemblyBuilderAccess::Run );
      ModuleBuilder^ moduleBuilder = assemblyBuilder->DefineDynamicModule( "MyDynamicModule" );
      TypeBuilder^ typeBuilder = moduleBuilder->DefineType( "MyDynamicType", TypeAttributes::Public );
      ConstructorBuilder^ constructorBuilder = typeBuilder->DefineConstructor( MethodAttributes::Public, CallingConventions::Standard, nullptr );
      ILGenerator^ ilGenerator = constructorBuilder->GetILGenerator();
      ilGenerator->EmitWriteLine( "MyDynamicType instantiated!" );
      ilGenerator->Emit( OpCodes::Ret );
      typeBuilder->CreateType();
      return assemblyBuilder;
   }

};

int main()
{
   AppDomain^ currentDomain = AppDomain::CurrentDomain;
   Test::InstantiateMyDynamicType( currentDomain ); // Failed!
   currentDomain->AssemblyResolve += gcnew ResolveEventHandler( Test::MyResolveEventHandler );
   Test::InstantiateMyDynamicType( currentDomain ); // OK!
}

using System;
using System.Reflection;
using System.Reflection.Emit;

class Test {
   public static void Main() {
      AppDomain currentDomain = AppDomain.CurrentDomain;

      InstantiateMyDynamicType(currentDomain);   // Failed!
      
      currentDomain.AssemblyResolve += new ResolveEventHandler(MyResolveEventHandler);
      
      InstantiateMyDynamicType(currentDomain);   // OK!
   }

   static void InstantiateMyDynamicType(AppDomain domain) {
      try {
         // You must supply a valid fully qualified assembly name here. 
         domain.CreateInstance("Assembly text name, Version, Culture, PublicKeyToken", "MyDynamicType");
      } catch (Exception e) {
         Console.WriteLine(e.Message);
      }
   }   

   static Assembly MyResolveEventHandler(object sender, ResolveEventArgs args) {
      return DefineDynamicAssembly((AppDomain) sender);
   }
   
   static Assembly DefineDynamicAssembly(AppDomain domain) {
      // Build a dynamic assembly using Reflection Emit API.
   
      AssemblyName assemblyName = new AssemblyName();
      assemblyName.Name = "MyDynamicAssembly";

      AssemblyBuilder assemblyBuilder = domain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run);
      ModuleBuilder moduleBuilder = assemblyBuilder.DefineDynamicModule("MyDynamicModule");
      TypeBuilder typeBuilder = moduleBuilder.DefineType("MyDynamicType", TypeAttributes.Public);
      ConstructorBuilder constructorBuilder = typeBuilder.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, null);
      ILGenerator ilGenerator = constructorBuilder.GetILGenerator();
      
      ilGenerator.EmitWriteLine("MyDynamicType instantiated!");
      ilGenerator.Emit(OpCodes.Ret);

      typeBuilder.CreateType();

      return assemblyBuilder;
   }
}

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

Module Test
   
   Sub Main()
      Dim currentDomain As AppDomain = AppDomain.CurrentDomain
      
      InstantiateMyDynamicType(currentDomain)   'Failed!
      
      AddHandler currentDomain.AssemblyResolve, AddressOf MyResolveEventHandler
      
      InstantiateMyDynamicType(currentDomain)   'OK!
   End Sub 'Main
   
   Sub InstantiateMyDynamicType(domain As AppDomain)
      Try
         ' You must supply a valid fully qualified assembly name here.
         domain.CreateInstance("Assembly text name, Version, Culture, PublicKeyToken", "MyDynamicType")
      Catch e As Exception
         Console.WriteLine(e.Message)
      End Try
   End Sub 'InstantiateMyDynamicType
   
   Function MyResolveEventHandler(sender As Object, args As ResolveEventArgs) As System.Reflection.Assembly
      Return DefineDynamicAssembly(DirectCast(sender, AppDomain))
   End Function 'MyResolveEventHandler
   
   Function DefineDynamicAssembly(domain As AppDomain) As System.Reflection.Assembly
      ' Build a dynamic assembly using Reflection Emit API.

      Dim assemblyName As New AssemblyName()
      assemblyName.Name = "MyDynamicAssembly"
      
      Dim assemblyBuilder As AssemblyBuilder = domain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run)
      Dim moduleBuilder As ModuleBuilder = assemblyBuilder.DefineDynamicModule("MyDynamicModule")
      Dim typeBuilder As TypeBuilder = moduleBuilder.DefineType("MyDynamicType", TypeAttributes.Public)
      Dim constructorBuilder As ConstructorBuilder = typeBuilder.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, Nothing)
      Dim ilGenerator As ILGenerator = constructorBuilder.GetILGenerator()
      
      ilGenerator.EmitWriteLine("MyDynamicType instantiated!")
      ilGenerator.Emit(OpCodes.Ret)
      
      typeBuilder.CreateType()
      
      Return assemblyBuilder
   End Function 'DefineDynamicAssembly

End Module 'Test 

注釈

指定されたアクセス許可要求requiredPermissionsoptionalPermissionsrefusedPermissions場合にだけ使用evidenceが指定されても、または動的アセンブリが保存され、メモリに再度読み込まれる場合。The permission requests specified for requiredPermissions, optionalPermissions, and refusedPermissions are used only if evidence is also supplied, or if the dynamic assembly is saved and reloaded into memory.

注意

動的アセンブリを生成するコードの開発中をお勧めを含めるようにSecurityPermissionFlag.SkipVerificationrefusedPermissionsします。During the development of code that emits dynamic assemblies, it is recommended that you include SecurityPermissionFlag.SkipVerification in refusedPermissions. 含むSkipVerificationで、refusedPermissionsパラメーターは、MSIL が検証されていることを保証します。Including SkipVerification in the refusedPermissions parameter ensures that the MSIL is verified. この技法の制限はまた、あるSecurityException完全な信頼を要求するコードで使用する場合にスローされます。A limitation of this technique is that it also causes SecurityException to be thrown when used with code that demands full trust.

動的な定義するときに、完全に信頼された呼び出し元は、証拠を提供できますのみAssemblyします。Only fully trusted callers can supply their evidence when defining a dynamic Assembly. マップは、ランタイム、Evidence付与されたアクセス許可を決定するセキュリティ ポリシーを使用します。The runtime will map the Evidence through the security policy to determine the granted permissions. 呼び出し元を指定する必要がありますを部分的に信頼されたnullevidenceパラメーター。Partially trusted callers must supply null for the evidence parameter. 場合evidencenull、ランタイムによってアクセス許可セットがコピー、現在の付与とから呼び出し元のセットを拒否Assemblyを動的Assembly定義されていると、ポリシーを解決済みとしてマークします。If evidence is null, the runtime copies the permission sets, that is, the current grant and deny sets, from the caller's Assembly to the dynamic Assembly being defined and marks policy as resolved.

場合、動的Assemblyがディスクに保存すると、その後の読み込みは、場所に関連付けられたポリシーに基づいて付与を取得は場所、Assemblyが保存されました。If the dynamic Assembly is saved to disk, subsequent loads will get grants based on policies associated with the location where the Assembly was saved.

場合isSynchronizedtrue、結果の次のメソッドAssemblyBuilderを同期する: DefineDynamicModuleDefineResourceAddResourceFileGetDynamicModuleSetEntryPoint、およびSaveします。If isSynchronized is true, the following methods of the resulting AssemblyBuilder will be synchronized: DefineDynamicModule, DefineResource, AddResourceFile, GetDynamicModule, SetEntryPoint, and Save. これらのメソッドの 2 つは、異なるスレッドで呼び出されると、もう一方が完了するまで 1 つブロックされます。If two of these methods are called on different threads, one will block until the other completes.

セキュリティ

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

DefineDynamicAssembly(AssemblyName, AssemblyBuilderAccess, String, Evidence, PermissionSet, PermissionSet, PermissionSet) DefineDynamicAssembly(AssemblyName, AssemblyBuilderAccess, String, Evidence, PermissionSet, PermissionSet, PermissionSet) DefineDynamicAssembly(AssemblyName, AssemblyBuilderAccess, String, Evidence, PermissionSet, PermissionSet, PermissionSet)

警告

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

名前、アクセス モード、ストレージ ディレクトリ、証拠、およびアクセス許可要求を指定して、動的アセンブリを定義します。Defines a dynamic assembly using the specified name, access mode, storage directory, evidence, and permission requests.

public:
 virtual System::Reflection::Emit::AssemblyBuilder ^ DefineDynamicAssembly(System::Reflection::AssemblyName ^ name, System::Reflection::Emit::AssemblyBuilderAccess access, System::String ^ dir, System::Security::Policy::Evidence ^ evidence, System::Security::PermissionSet ^ requiredPermissions, System::Security::PermissionSet ^ optionalPermissions, System::Security::PermissionSet ^ refusedPermissions);
[System.Obsolete("Declarative security for assembly level is no longer enforced")]
[System.Obsolete("Assembly level declarative security is obsolete and is no longer enforced by the CLR by default.  Please see http://go.microsoft.com/fwlink/?LinkId=155570 for more information.")]
public System.Reflection.Emit.AssemblyBuilder DefineDynamicAssembly (System.Reflection.AssemblyName name, System.Reflection.Emit.AssemblyBuilderAccess access, string dir, System.Security.Policy.Evidence evidence, System.Security.PermissionSet requiredPermissions, System.Security.PermissionSet optionalPermissions, System.Security.PermissionSet refusedPermissions);
abstract member DefineDynamicAssembly : System.Reflection.AssemblyName * System.Reflection.Emit.AssemblyBuilderAccess * string * System.Security.Policy.Evidence * System.Security.PermissionSet * System.Security.PermissionSet * System.Security.PermissionSet -> System.Reflection.Emit.AssemblyBuilder
override this.DefineDynamicAssembly : System.Reflection.AssemblyName * System.Reflection.Emit.AssemblyBuilderAccess * string * System.Security.Policy.Evidence * System.Security.PermissionSet * System.Security.PermissionSet * System.Security.PermissionSet -> System.Reflection.Emit.AssemblyBuilder

パラメーター

name
AssemblyName AssemblyName AssemblyName AssemblyName

動的アセンブリの一意の ID。The unique identity of the dynamic assembly.

access
AssemblyBuilderAccess AssemblyBuilderAccess AssemblyBuilderAccess AssemblyBuilderAccess

動的アセンブリにアクセスするときのモード。The mode in which the dynamic assembly will be accessed.

dir
String String String String

アセンブリを保存するディレクトリの名前。The name of the directory where the assembly will be saved. dirnull の場合は、現在のディレクトリが既定で使用されます。If dir is null, the directory defaults to the current directory.

evidence
Evidence Evidence Evidence Evidence

動的アセンブリに指定する証拠。The evidence supplied for the dynamic assembly. 証拠は、ポリシーの解決に使用する証拠の最終セットを変更されないために使用されます。The evidence is used unaltered as the final set of evidence used for policy resolution.

requiredPermissions
PermissionSet PermissionSet PermissionSet PermissionSet

必須のアクセス許可要求。The required permissions request.

optionalPermissions
PermissionSet PermissionSet PermissionSet PermissionSet

オプションのアクセス許可要求。The optional permissions request.

refusedPermissions
PermissionSet PermissionSet PermissionSet PermissionSet

拒否されたアクセス許可要求。The refused permissions request.

戻り値

名前と機能が指定された動的アセンブリ。A dynamic assembly with the specified name and features.

実装

例外

nameName プロパティが null です。The Name property of name is null.

または-or- nameName プロパティが空白文字で始まるか、スラッシュまたはバック スラッシュを含んでいます。The Name property of name begins with white space, or contains a forward or backward slash.

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

次のサンプルでは、DefineDynamicAssemblyメソッドとAssemblyResolveイベント。The following sample demonstrates the DefineDynamicAssembly method and AssemblyResolve event.

このコード例を実行するには、完全修飾アセンブリ名を指定する必要があります。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::Reflection;
using namespace System::Reflection::Emit;
ref class Test
{
public:
   static void InstantiateMyDynamicType( AppDomain^ domain )
   {
      try
      {
         
         // You must supply a valid fully qualified assembly name here.
         domain->CreateInstance( "Assembly text name, Version, Culture, PublicKeyToken", "MyDynamicType" );
      }
      catch ( Exception^ e ) 
      {
         Console::WriteLine( e->Message );
      }

   }

   static Assembly^ MyResolveEventHandler( Object^ sender, ResolveEventArgs^ args )
   {
      return DefineDynamicAssembly( dynamic_cast<AppDomain^>(sender) );
   }

   static Assembly^ DefineDynamicAssembly( AppDomain^ domain )
   {
      
      // Build a dynamic assembly using Reflection Emit API.
      AssemblyName^ assemblyName = gcnew AssemblyName;
      assemblyName->Name = "MyDynamicAssembly";
      AssemblyBuilder^ assemblyBuilder = domain->DefineDynamicAssembly( assemblyName, AssemblyBuilderAccess::Run );
      ModuleBuilder^ moduleBuilder = assemblyBuilder->DefineDynamicModule( "MyDynamicModule" );
      TypeBuilder^ typeBuilder = moduleBuilder->DefineType( "MyDynamicType", TypeAttributes::Public );
      ConstructorBuilder^ constructorBuilder = typeBuilder->DefineConstructor( MethodAttributes::Public, CallingConventions::Standard, nullptr );
      ILGenerator^ ilGenerator = constructorBuilder->GetILGenerator();
      ilGenerator->EmitWriteLine( "MyDynamicType instantiated!" );
      ilGenerator->Emit( OpCodes::Ret );
      typeBuilder->CreateType();
      return assemblyBuilder;
   }

};

int main()
{
   AppDomain^ currentDomain = AppDomain::CurrentDomain;
   Test::InstantiateMyDynamicType( currentDomain ); // Failed!
   currentDomain->AssemblyResolve += gcnew ResolveEventHandler( Test::MyResolveEventHandler );
   Test::InstantiateMyDynamicType( currentDomain ); // OK!
}

using System;
using System.Reflection;
using System.Reflection.Emit;

class Test {
   public static void Main() {
      AppDomain currentDomain = AppDomain.CurrentDomain;

      InstantiateMyDynamicType(currentDomain);   // Failed!
      
      currentDomain.AssemblyResolve += new ResolveEventHandler(MyResolveEventHandler);
      
      InstantiateMyDynamicType(currentDomain);   // OK!
   }

   static void InstantiateMyDynamicType(AppDomain domain) {
      try {
         // You must supply a valid fully qualified assembly name here. 
         domain.CreateInstance("Assembly text name, Version, Culture, PublicKeyToken", "MyDynamicType");
      } catch (Exception e) {
         Console.WriteLine(e.Message);
      }
   }   

   static Assembly MyResolveEventHandler(object sender, ResolveEventArgs args) {
      return DefineDynamicAssembly((AppDomain) sender);
   }
   
   static Assembly DefineDynamicAssembly(AppDomain domain) {
      // Build a dynamic assembly using Reflection Emit API.
   
      AssemblyName assemblyName = new AssemblyName();
      assemblyName.Name = "MyDynamicAssembly";

      AssemblyBuilder assemblyBuilder = domain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run);
      ModuleBuilder moduleBuilder = assemblyBuilder.DefineDynamicModule("MyDynamicModule");
      TypeBuilder typeBuilder = moduleBuilder.DefineType("MyDynamicType", TypeAttributes.Public);
      ConstructorBuilder constructorBuilder = typeBuilder.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, null);
      ILGenerator ilGenerator = constructorBuilder.GetILGenerator();
      
      ilGenerator.EmitWriteLine("MyDynamicType instantiated!");
      ilGenerator.Emit(OpCodes.Ret);

      typeBuilder.CreateType();

      return assemblyBuilder;
   }
}

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

Module Test
   
   Sub Main()
      Dim currentDomain As AppDomain = AppDomain.CurrentDomain
      
      InstantiateMyDynamicType(currentDomain)   'Failed!
      
      AddHandler currentDomain.AssemblyResolve, AddressOf MyResolveEventHandler
      
      InstantiateMyDynamicType(currentDomain)   'OK!
   End Sub 'Main
   
   Sub InstantiateMyDynamicType(domain As AppDomain)
      Try
         ' You must supply a valid fully qualified assembly name here.
         domain.CreateInstance("Assembly text name, Version, Culture, PublicKeyToken", "MyDynamicType")
      Catch e As Exception
         Console.WriteLine(e.Message)
      End Try
   End Sub 'InstantiateMyDynamicType
   
   Function MyResolveEventHandler(sender As Object, args As ResolveEventArgs) As System.Reflection.Assembly
      Return DefineDynamicAssembly(DirectCast(sender, AppDomain))
   End Function 'MyResolveEventHandler
   
   Function DefineDynamicAssembly(domain As AppDomain) As System.Reflection.Assembly
      ' Build a dynamic assembly using Reflection Emit API.

      Dim assemblyName As New AssemblyName()
      assemblyName.Name = "MyDynamicAssembly"
      
      Dim assemblyBuilder As AssemblyBuilder = domain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run)
      Dim moduleBuilder As ModuleBuilder = assemblyBuilder.DefineDynamicModule("MyDynamicModule")
      Dim typeBuilder As TypeBuilder = moduleBuilder.DefineType("MyDynamicType", TypeAttributes.Public)
      Dim constructorBuilder As ConstructorBuilder = typeBuilder.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, Nothing)
      Dim ilGenerator As ILGenerator = constructorBuilder.GetILGenerator()
      
      ilGenerator.EmitWriteLine("MyDynamicType instantiated!")
      ilGenerator.Emit(OpCodes.Ret)
      
      typeBuilder.CreateType()
      
      Return assemblyBuilder
   End Function 'DefineDynamicAssembly

End Module 'Test 

注釈

指定されたアクセス許可要求requiredPermissionsoptionalPermissionsrefusedPermissions場合にだけ使用evidenceが指定されても、または動的アセンブリが保存され、メモリに再度読み込まれる場合。The permission requests specified for requiredPermissions, optionalPermissions, and refusedPermissions are used only if evidence is also supplied, or if the dynamic assembly is saved and reloaded into memory.

注意

動的アセンブリを生成するコードの開発中をお勧めを含めるようにSecurityPermissionFlag.SkipVerificationrefusedPermissionsします。During the development of code that emits dynamic assemblies, it is recommended that you include SecurityPermissionFlag.SkipVerification in refusedPermissions. 含むSkipVerificationで、refusedPermissionsパラメーターは、MSIL が検証されていることを保証します。Including SkipVerification in the refusedPermissions parameter ensures that the MSIL is verified. この技法の制限はまた、あるSecurityException完全な信頼を要求するコードで使用する場合にスローされます。A limitation of this technique is that it also causes SecurityException to be thrown when used with code that demands full trust.

のみ完全に信頼された呼び出し元を指定できます、evidence動的を定義するときにAssemblyします。Only fully trusted callers can supply their evidence when defining a dynamic Assembly. マップは、ランタイム、Evidence付与されたアクセス許可を決定するセキュリティ ポリシーを使用します。The runtime will map the Evidence through the security policy to determine the granted permissions. 呼び出し元は、null を指定する必要がありますを部分的に信頼されたevidenceします。Partially trusted callers must supply a null evidence. 場合evidencenull、ランタイムによってアクセス許可セットがコピー、現在の付与とから呼び出し元のセットを拒否Assemblyを動的Assembly定義されていると、ポリシーを解決済みとしてマークします。If evidence is null, the runtime copies the permission sets, that is, the current grant and deny sets, from the caller's Assembly to the dynamic Assembly being defined and marks policy as resolved.

場合、動的Assemblyがディスクに保存すると、その後の読み込みは、場所に関連付けられたポリシーに基づいて付与を取得は場所、Assemblyが保存されました。If the dynamic Assembly is saved to disk, subsequent loads will get grants based on policies associated with the location where the Assembly was saved.

このメソッドは、現在のアプリケーション ドメインで、動的アセンブリを定義するのみ使用する必要があります。This method should only be used to define a dynamic assembly in the current application domain. 詳細については、次を参照してください。、Load(AssemblyName)メソッドのオーバー ロードします。For more information, see the Load(AssemblyName) method overload.

セキュリティ

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

DefineDynamicAssembly(AssemblyName, AssemblyBuilderAccess, String, PermissionSet, PermissionSet, PermissionSet) DefineDynamicAssembly(AssemblyName, AssemblyBuilderAccess, String, PermissionSet, PermissionSet, PermissionSet) DefineDynamicAssembly(AssemblyName, AssemblyBuilderAccess, String, PermissionSet, PermissionSet, PermissionSet) DefineDynamicAssembly(AssemblyName, AssemblyBuilderAccess, String, PermissionSet, PermissionSet, PermissionSet)

警告

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

名前、アクセス モード、ストレージ ディレクトリ、およびアクセス許可要求を指定して、動的アセンブリを定義します。Defines a dynamic assembly using the specified name, access mode, storage directory, and permission requests.

public:
 virtual System::Reflection::Emit::AssemblyBuilder ^ DefineDynamicAssembly(System::Reflection::AssemblyName ^ name, System::Reflection::Emit::AssemblyBuilderAccess access, System::String ^ dir, System::Security::PermissionSet ^ requiredPermissions, System::Security::PermissionSet ^ optionalPermissions, System::Security::PermissionSet ^ refusedPermissions);
[System.Obsolete("Declarative security for assembly level is no longer enforced")]
[System.Obsolete("Assembly level declarative security is obsolete and is no longer enforced by the CLR by default. See http://go.microsoft.com/fwlink/?LinkID=155570 for more information.")]
public System.Reflection.Emit.AssemblyBuilder DefineDynamicAssembly (System.Reflection.AssemblyName name, System.Reflection.Emit.AssemblyBuilderAccess access, string dir, System.Security.PermissionSet requiredPermissions, System.Security.PermissionSet optionalPermissions, System.Security.PermissionSet refusedPermissions);
abstract member DefineDynamicAssembly : System.Reflection.AssemblyName * System.Reflection.Emit.AssemblyBuilderAccess * string * System.Security.PermissionSet * System.Security.PermissionSet * System.Security.PermissionSet -> System.Reflection.Emit.AssemblyBuilder
override this.DefineDynamicAssembly : System.Reflection.AssemblyName * System.Reflection.Emit.AssemblyBuilderAccess * string * System.Security.PermissionSet * System.Security.PermissionSet * System.Security.PermissionSet -> System.Reflection.Emit.AssemblyBuilder
Public Function DefineDynamicAssembly (name As AssemblyName, access As AssemblyBuilderAccess, dir As String, requiredPermissions As PermissionSet, optionalPermissions As PermissionSet, refusedPermissions As PermissionSet) As AssemblyBuilder

パラメーター

name
AssemblyName AssemblyName AssemblyName AssemblyName

動的アセンブリの一意の ID。The unique identity of the dynamic assembly.

access
AssemblyBuilderAccess AssemblyBuilderAccess AssemblyBuilderAccess AssemblyBuilderAccess

動的アセンブリにアクセスするときのモード。The mode in which the dynamic assembly will be accessed.

dir
String String String String

アセンブリを保存するディレクトリの名前。The name of the directory where the assembly will be saved. dirnull の場合は、現在のディレクトリが既定で使用されます。If dir is null, the directory defaults to the current directory.

requiredPermissions
PermissionSet PermissionSet PermissionSet PermissionSet

必須のアクセス許可要求。The required permissions request.

optionalPermissions
PermissionSet PermissionSet PermissionSet PermissionSet

オプションのアクセス許可要求。The optional permissions request.

refusedPermissions
PermissionSet PermissionSet PermissionSet PermissionSet

拒否されたアクセス許可要求。The refused permissions request.

戻り値

名前と機能が指定された動的アセンブリ。A dynamic assembly with the specified name and features.

実装

例外

nameName プロパティが null です。The Name property of name is null.

または-or- nameName プロパティが空白文字で始まるか、スラッシュまたはバック スラッシュを含んでいます。The Name property of name begins with white space, or contains a forward or backward slash.

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

次のサンプルでは、DefineDynamicAssemblyメソッドとAssemblyResolveイベント。The following sample demonstrates the DefineDynamicAssembly method and AssemblyResolve event.

このコード例を実行するには、完全修飾アセンブリ名を指定する必要があります。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::Reflection;
using namespace System::Reflection::Emit;
ref class Test
{
public:
   static void InstantiateMyDynamicType( AppDomain^ domain )
   {
      try
      {
         
         // You must supply a valid fully qualified assembly name here.
         domain->CreateInstance( "Assembly text name, Version, Culture, PublicKeyToken", "MyDynamicType" );
      }
      catch ( Exception^ e ) 
      {
         Console::WriteLine( e->Message );
      }

   }

   static Assembly^ MyResolveEventHandler( Object^ sender, ResolveEventArgs^ args )
   {
      return DefineDynamicAssembly( dynamic_cast<AppDomain^>(sender) );
   }

   static Assembly^ DefineDynamicAssembly( AppDomain^ domain )
   {
      
      // Build a dynamic assembly using Reflection Emit API.
      AssemblyName^ assemblyName = gcnew AssemblyName;
      assemblyName->Name = "MyDynamicAssembly";
      AssemblyBuilder^ assemblyBuilder = domain->DefineDynamicAssembly( assemblyName, AssemblyBuilderAccess::Run );
      ModuleBuilder^ moduleBuilder = assemblyBuilder->DefineDynamicModule( "MyDynamicModule" );
      TypeBuilder^ typeBuilder = moduleBuilder->DefineType( "MyDynamicType", TypeAttributes::Public );
      ConstructorBuilder^ constructorBuilder = typeBuilder->DefineConstructor( MethodAttributes::Public, CallingConventions::Standard, nullptr );
      ILGenerator^ ilGenerator = constructorBuilder->GetILGenerator();
      ilGenerator->EmitWriteLine( "MyDynamicType instantiated!" );
      ilGenerator->Emit( OpCodes::Ret );
      typeBuilder->CreateType();
      return assemblyBuilder;
   }

};

int main()
{
   AppDomain^ currentDomain = AppDomain::CurrentDomain;
   Test::InstantiateMyDynamicType( currentDomain ); // Failed!
   currentDomain->AssemblyResolve += gcnew ResolveEventHandler( Test::MyResolveEventHandler );
   Test::InstantiateMyDynamicType( currentDomain ); // OK!
}

using System;
using System.Reflection;
using System.Reflection.Emit;

class Test {
   public static void Main() {
      AppDomain currentDomain = AppDomain.CurrentDomain;

      InstantiateMyDynamicType(currentDomain);   // Failed!
      
      currentDomain.AssemblyResolve += new ResolveEventHandler(MyResolveEventHandler);
      
      InstantiateMyDynamicType(currentDomain);   // OK!
   }

   static void InstantiateMyDynamicType(AppDomain domain) {
      try {
         // You must supply a valid fully qualified assembly name here. 
         domain.CreateInstance("Assembly text name, Version, Culture, PublicKeyToken", "MyDynamicType");
      } catch (Exception e) {
         Console.WriteLine(e.Message);
      }
   }   

   static Assembly MyResolveEventHandler(object sender, ResolveEventArgs args) {
      return DefineDynamicAssembly((AppDomain) sender);
   }
   
   static Assembly DefineDynamicAssembly(AppDomain domain) {
      // Build a dynamic assembly using Reflection Emit API.
   
      AssemblyName assemblyName = new AssemblyName();
      assemblyName.Name = "MyDynamicAssembly";

      AssemblyBuilder assemblyBuilder = domain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run);
      ModuleBuilder moduleBuilder = assemblyBuilder.DefineDynamicModule("MyDynamicModule");
      TypeBuilder typeBuilder = moduleBuilder.DefineType("MyDynamicType", TypeAttributes.Public);
      ConstructorBuilder constructorBuilder = typeBuilder.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, null);
      ILGenerator ilGenerator = constructorBuilder.GetILGenerator();
      
      ilGenerator.EmitWriteLine("MyDynamicType instantiated!");
      ilGenerator.Emit(OpCodes.Ret);

      typeBuilder.CreateType();

      return assemblyBuilder;
   }
}

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

Module Test
   
   Sub Main()
      Dim currentDomain As AppDomain = AppDomain.CurrentDomain
      
      InstantiateMyDynamicType(currentDomain)   'Failed!
      
      AddHandler currentDomain.AssemblyResolve, AddressOf MyResolveEventHandler
      
      InstantiateMyDynamicType(currentDomain)   'OK!
   End Sub 'Main
   
   Sub InstantiateMyDynamicType(domain As AppDomain)
      Try
         ' You must supply a valid fully qualified assembly name here.
         domain.CreateInstance("Assembly text name, Version, Culture, PublicKeyToken", "MyDynamicType")
      Catch e As Exception
         Console.WriteLine(e.Message)
      End Try
   End Sub 'InstantiateMyDynamicType
   
   Function MyResolveEventHandler(sender As Object, args As ResolveEventArgs) As System.Reflection.Assembly
      Return DefineDynamicAssembly(DirectCast(sender, AppDomain))
   End Function 'MyResolveEventHandler
   
   Function DefineDynamicAssembly(domain As AppDomain) As System.Reflection.Assembly
      ' Build a dynamic assembly using Reflection Emit API.

      Dim assemblyName As New AssemblyName()
      assemblyName.Name = "MyDynamicAssembly"
      
      Dim assemblyBuilder As AssemblyBuilder = domain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run)
      Dim moduleBuilder As ModuleBuilder = assemblyBuilder.DefineDynamicModule("MyDynamicModule")
      Dim typeBuilder As TypeBuilder = moduleBuilder.DefineType("MyDynamicType", TypeAttributes.Public)
      Dim constructorBuilder As ConstructorBuilder = typeBuilder.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, Nothing)
      Dim ilGenerator As ILGenerator = constructorBuilder.GetILGenerator()
      
      ilGenerator.EmitWriteLine("MyDynamicType instantiated!")
      ilGenerator.Emit(OpCodes.Ret)
      
      typeBuilder.CreateType()
      
      Return assemblyBuilder
   End Function 'DefineDynamicAssembly

End Module 'Test 

注釈

指定されたアクセス許可要求requiredPermissionsoptionalPermissions、およびrefusedPermissions動的アセンブリに保存され、メモリに再読み込みしない限りは使用されません。The permission requests specified for requiredPermissions, optionalPermissions, and refusedPermissions are not used unless the dynamic assembly has been saved and reloaded into memory. ディスクのオーバー ロードを使用することはありません保存されている一時アセンブリのアクセス許可要求を指定する、DefineDynamicAssemblyメソッドも、要求されたアクセス許可、および供給としての証拠を示す、Evidenceオブジェクト。To specify permission requests for a transient assembly that is never saved to disk, use an overload of the DefineDynamicAssembly method that specifies evidence as well as requested permissions, and supply an Evidence object.

注意

動的アセンブリを生成するコードの開発中はお勧めのオーバー ロードを使用すること、DefineDynamicAssembly証拠、アクセス許可を指定するメソッドを含む動的アセンブリを作成証拠を指定するSecurityPermissionFlag.SkipVerificationrefusedPermissions.During the development of code that emits dynamic assemblies, it is recommended that you use an overload of the DefineDynamicAssembly method that specifies evidence and permissions, supply the evidence you want the dynamic assembly to have, and include SecurityPermissionFlag.SkipVerification in refusedPermissions. 含むSkipVerificationで、refusedPermissionsパラメーターは、MSIL が検証されていることを保証します。Including SkipVerification in the refusedPermissions parameter ensures that the MSIL is verified. この技法の制限はまた、あるSecurityException完全な信頼を要求するコードで使用する場合にスローされます。A limitation of this technique is that it also causes SecurityException to be thrown when used with code that demands full trust.

このメソッドは、現在のアプリケーション ドメインで、動的アセンブリを定義するのみ使用する必要があります。This method should only be used to define a dynamic assembly in the current application domain. 詳細については、次を参照してください。、Load(AssemblyName)メソッドのオーバー ロードします。For more information, see the Load(AssemblyName) method overload.

DefineDynamicAssembly(AssemblyName, AssemblyBuilderAccess, Evidence, PermissionSet, PermissionSet, PermissionSet) DefineDynamicAssembly(AssemblyName, AssemblyBuilderAccess, Evidence, PermissionSet, PermissionSet, PermissionSet) DefineDynamicAssembly(AssemblyName, AssemblyBuilderAccess, Evidence, PermissionSet, PermissionSet, PermissionSet)

警告

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

名前、アクセス モード、証拠、およびアクセス許可要求を指定して、動的アセンブリを定義します。Defines a dynamic assembly using the specified name, access mode, evidence, and permission requests.

public:
 virtual System::Reflection::Emit::AssemblyBuilder ^ DefineDynamicAssembly(System::Reflection::AssemblyName ^ name, System::Reflection::Emit::AssemblyBuilderAccess access, System::Security::Policy::Evidence ^ evidence, System::Security::PermissionSet ^ requiredPermissions, System::Security::PermissionSet ^ optionalPermissions, System::Security::PermissionSet ^ refusedPermissions);
[System.Obsolete("Declarative security for assembly level is no longer enforced")]
[System.Obsolete("Assembly level declarative security is obsolete and is no longer enforced by the CLR by default. See http://go.microsoft.com/fwlink/?LinkID=155570 for more information.")]
public System.Reflection.Emit.AssemblyBuilder DefineDynamicAssembly (System.Reflection.AssemblyName name, System.Reflection.Emit.AssemblyBuilderAccess access, System.Security.Policy.Evidence evidence, System.Security.PermissionSet requiredPermissions, System.Security.PermissionSet optionalPermissions, System.Security.PermissionSet refusedPermissions);
abstract member DefineDynamicAssembly : System.Reflection.AssemblyName * System.Reflection.Emit.AssemblyBuilderAccess * System.Security.Policy.Evidence * System.Security.PermissionSet * System.Security.PermissionSet * System.Security.PermissionSet -> System.Reflection.Emit.AssemblyBuilder
override this.DefineDynamicAssembly : System.Reflection.AssemblyName * System.Reflection.Emit.AssemblyBuilderAccess * System.Security.Policy.Evidence * System.Security.PermissionSet * System.Security.PermissionSet * System.Security.PermissionSet -> System.Reflection.Emit.AssemblyBuilder

パラメーター

name
AssemblyName AssemblyName AssemblyName AssemblyName

動的アセンブリの一意の ID。The unique identity of the dynamic assembly.

access
AssemblyBuilderAccess AssemblyBuilderAccess AssemblyBuilderAccess AssemblyBuilderAccess

動的アセンブリにアクセスするときのモード。The mode in which the dynamic assembly will be accessed.

evidence
Evidence Evidence Evidence Evidence

動的アセンブリに指定する証拠。The evidence supplied for the dynamic assembly. 証拠は、ポリシーの解決に使用する証拠の最終セットを変更されないために使用されます。The evidence is used unaltered as the final set of evidence used for policy resolution.

requiredPermissions
PermissionSet PermissionSet PermissionSet PermissionSet

必須のアクセス許可要求。The required permissions request.

optionalPermissions
PermissionSet PermissionSet PermissionSet PermissionSet

オプションのアクセス許可要求。The optional permissions request.

refusedPermissions
PermissionSet PermissionSet PermissionSet PermissionSet

拒否されたアクセス許可要求。The refused permissions request.

戻り値

名前と機能が指定された動的アセンブリ。A dynamic assembly with the specified name and features.

実装

例外

nameName プロパティが null です。The Name property of name is null.

または-or- nameName プロパティが空白文字で始まるか、スラッシュまたはバック スラッシュを含んでいます。The Name property of name begins with white space, or contains a forward or backward slash.

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

次のサンプルでは、DefineDynamicAssemblyメソッドとAssemblyResolveイベント。The following sample demonstrates the DefineDynamicAssembly method and AssemblyResolve event.

このコード例を実行するには、完全修飾アセンブリ名を指定する必要があります。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::Reflection;
using namespace System::Reflection::Emit;
ref class Test
{
public:
   static void InstantiateMyDynamicType( AppDomain^ domain )
   {
      try
      {
         
         // You must supply a valid fully qualified assembly name here.
         domain->CreateInstance( "Assembly text name, Version, Culture, PublicKeyToken", "MyDynamicType" );
      }
      catch ( Exception^ e ) 
      {
         Console::WriteLine( e->Message );
      }

   }

   static Assembly^ MyResolveEventHandler( Object^ sender, ResolveEventArgs^ args )
   {
      return DefineDynamicAssembly( dynamic_cast<AppDomain^>(sender) );
   }

   static Assembly^ DefineDynamicAssembly( AppDomain^ domain )
   {
      
      // Build a dynamic assembly using Reflection Emit API.
      AssemblyName^ assemblyName = gcnew AssemblyName;
      assemblyName->Name = "MyDynamicAssembly";
      AssemblyBuilder^ assemblyBuilder = domain->DefineDynamicAssembly( assemblyName, AssemblyBuilderAccess::Run );
      ModuleBuilder^ moduleBuilder = assemblyBuilder->DefineDynamicModule( "MyDynamicModule" );
      TypeBuilder^ typeBuilder = moduleBuilder->DefineType( "MyDynamicType", TypeAttributes::Public );
      ConstructorBuilder^ constructorBuilder = typeBuilder->DefineConstructor( MethodAttributes::Public, CallingConventions::Standard, nullptr );
      ILGenerator^ ilGenerator = constructorBuilder->GetILGenerator();
      ilGenerator->EmitWriteLine( "MyDynamicType instantiated!" );
      ilGenerator->Emit( OpCodes::Ret );
      typeBuilder->CreateType();
      return assemblyBuilder;
   }

};

int main()
{
   AppDomain^ currentDomain = AppDomain::CurrentDomain;
   Test::InstantiateMyDynamicType( currentDomain ); // Failed!
   currentDomain->AssemblyResolve += gcnew ResolveEventHandler( Test::MyResolveEventHandler );
   Test::InstantiateMyDynamicType( currentDomain ); // OK!
}

using System;
using System.Reflection;
using System.Reflection.Emit;

class Test {
   public static void Main() {
      AppDomain currentDomain = AppDomain.CurrentDomain;

      InstantiateMyDynamicType(currentDomain);   // Failed!
      
      currentDomain.AssemblyResolve += new ResolveEventHandler(MyResolveEventHandler);
      
      InstantiateMyDynamicType(currentDomain);   // OK!
   }

   static void InstantiateMyDynamicType(AppDomain domain) {
      try {
         // You must supply a valid fully qualified assembly name here. 
         domain.CreateInstance("Assembly text name, Version, Culture, PublicKeyToken", "MyDynamicType");
      } catch (Exception e) {
         Console.WriteLine(e.Message);
      }
   }   

   static Assembly MyResolveEventHandler(object sender, ResolveEventArgs args) {
      return DefineDynamicAssembly((AppDomain) sender);
   }
   
   static Assembly DefineDynamicAssembly(AppDomain domain) {
      // Build a dynamic assembly using Reflection Emit API.
   
      AssemblyName assemblyName = new AssemblyName();
      assemblyName.Name = "MyDynamicAssembly";

      AssemblyBuilder assemblyBuilder = domain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run);
      ModuleBuilder moduleBuilder = assemblyBuilder.DefineDynamicModule("MyDynamicModule");
      TypeBuilder typeBuilder = moduleBuilder.DefineType("MyDynamicType", TypeAttributes.Public);
      ConstructorBuilder constructorBuilder = typeBuilder.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, null);
      ILGenerator ilGenerator = constructorBuilder.GetILGenerator();
      
      ilGenerator.EmitWriteLine("MyDynamicType instantiated!");
      ilGenerator.Emit(OpCodes.Ret);

      typeBuilder.CreateType();

      return assemblyBuilder;
   }
}

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

Module Test
   
   Sub Main()
      Dim currentDomain As AppDomain = AppDomain.CurrentDomain
      
      InstantiateMyDynamicType(currentDomain)   'Failed!
      
      AddHandler currentDomain.AssemblyResolve, AddressOf MyResolveEventHandler
      
      InstantiateMyDynamicType(currentDomain)   'OK!
   End Sub 'Main
   
   Sub InstantiateMyDynamicType(domain As AppDomain)
      Try
         ' You must supply a valid fully qualified assembly name here.
         domain.CreateInstance("Assembly text name, Version, Culture, PublicKeyToken", "MyDynamicType")
      Catch e As Exception
         Console.WriteLine(e.Message)
      End Try
   End Sub 'InstantiateMyDynamicType
   
   Function MyResolveEventHandler(sender As Object, args As ResolveEventArgs) As System.Reflection.Assembly
      Return DefineDynamicAssembly(DirectCast(sender, AppDomain))
   End Function 'MyResolveEventHandler
   
   Function DefineDynamicAssembly(domain As AppDomain) As System.Reflection.Assembly
      ' Build a dynamic assembly using Reflection Emit API.

      Dim assemblyName As New AssemblyName()
      assemblyName.Name = "MyDynamicAssembly"
      
      Dim assemblyBuilder As AssemblyBuilder = domain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run)
      Dim moduleBuilder As ModuleBuilder = assemblyBuilder.DefineDynamicModule("MyDynamicModule")
      Dim typeBuilder As TypeBuilder = moduleBuilder.DefineType("MyDynamicType", TypeAttributes.Public)
      Dim constructorBuilder As ConstructorBuilder = typeBuilder.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, Nothing)
      Dim ilGenerator As ILGenerator = constructorBuilder.GetILGenerator()
      
      ilGenerator.EmitWriteLine("MyDynamicType instantiated!")
      ilGenerator.Emit(OpCodes.Ret)
      
      typeBuilder.CreateType()
      
      Return assemblyBuilder
   End Function 'DefineDynamicAssembly

End Module 'Test 

注釈

指定されたアクセス許可要求requiredPermissionsoptionalPermissionsrefusedPermissions場合にだけ使用evidenceが指定されても、または動的アセンブリが保存され、メモリに再度読み込まれる場合。The permission requests specified for requiredPermissions, optionalPermissions, and refusedPermissions are used only if evidence is also supplied, or if the dynamic assembly is saved and reloaded into memory.

注意

動的アセンブリを生成するコードの開発中をお勧めを含めるようにSecurityPermissionFlag.SkipVerificationrefusedPermissionsします。During the development of code that emits dynamic assemblies, it is recommended that you include SecurityPermissionFlag.SkipVerification in refusedPermissions. 含むSkipVerificationで、refusedPermissionsパラメーターは、MSIL が検証されていることを保証します。Including SkipVerification in the refusedPermissions parameter ensures that the MSIL is verified. この技法の制限はまた、あるSecurityException完全な信頼を要求するコードで使用する場合にスローされます。A limitation of this technique is that it also causes SecurityException to be thrown when used with code that demands full trust.

のみ完全に信頼された呼び出し元を指定できます、evidence動的を定義するときにAssemblyします。Only fully trusted callers can supply their evidence when defining a dynamic Assembly. マップは、ランタイム、Evidence付与されたアクセス許可を決定するセキュリティ ポリシーを使用します。The runtime will map the Evidence through the security policy to determine the granted permissions. 呼び出し元は、null を指定する必要がありますを部分的に信頼されたevidenceします。Partially trusted callers must supply a null evidence. 場合evidencenull、ランタイムによってアクセス許可セットがコピー、現在の付与とから呼び出し元のセットを拒否Assemblyを動的Assembly定義されていると、ポリシーを解決済みとしてマークします。If evidence is null, the runtime copies the permission sets, that is, the current grant and deny sets, from the caller's Assembly to the dynamic Assembly being defined and marks policy as resolved.

場合、動的Assemblyがディスクに保存すると、その後の読み込みは、場所に関連付けられたポリシーに基づいて付与を取得は場所、Assemblyが保存されました。If the dynamic Assembly is saved to disk, subsequent loads will get grants based on policies associated with the location where the Assembly was saved.

このメソッドは、現在のアプリケーション ドメインで、動的アセンブリを定義するのみ使用する必要があります。This method should only be used to define a dynamic assembly in the current application domain. 詳細については、次を参照してください。、Load(AssemblyName)メソッドのオーバー ロードします。For more information, see the Load(AssemblyName) method overload.

セキュリティ

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

DefineDynamicAssembly(AssemblyName, AssemblyBuilderAccess, String, Boolean, IEnumerable<CustomAttributeBuilder>) DefineDynamicAssembly(AssemblyName, AssemblyBuilderAccess, String, Boolean, IEnumerable<CustomAttributeBuilder>) DefineDynamicAssembly(AssemblyName, AssemblyBuilderAccess, String, Boolean, IEnumerable<CustomAttributeBuilder>) DefineDynamicAssembly(AssemblyName, AssemblyBuilderAccess, String, Boolean, IEnumerable<CustomAttributeBuilder>)

名前、アクセス モード、ストレージ ディレクトリ、および同期オプションを指定して、動的アセンブリを定義します。Defines a dynamic assembly using the specified name, access mode, storage directory, and synchronization option.

public:
 System::Reflection::Emit::AssemblyBuilder ^ DefineDynamicAssembly(System::Reflection::AssemblyName ^ name, System::Reflection::Emit::AssemblyBuilderAccess access, System::String ^ dir, bool isSynchronized, System::Collections::Generic::IEnumerable<System::Reflection::Emit::CustomAttributeBuilder ^> ^ assemblyAttributes);
public System.Reflection.Emit.AssemblyBuilder DefineDynamicAssembly (System.Reflection.AssemblyName name, System.Reflection.Emit.AssemblyBuilderAccess access, string dir, bool isSynchronized, System.Collections.Generic.IEnumerable<System.Reflection.Emit.CustomAttributeBuilder> assemblyAttributes);
member this.DefineDynamicAssembly : System.Reflection.AssemblyName * System.Reflection.Emit.AssemblyBuilderAccess * string * bool * seq<System.Reflection.Emit.CustomAttributeBuilder> -> System.Reflection.Emit.AssemblyBuilder
Public Function DefineDynamicAssembly (name As AssemblyName, access As AssemblyBuilderAccess, dir As String, isSynchronized As Boolean, assemblyAttributes As IEnumerable(Of CustomAttributeBuilder)) As AssemblyBuilder

パラメーター

name
AssemblyName AssemblyName AssemblyName AssemblyName

動的アセンブリの一意の ID。The unique identity of the dynamic assembly.

access
AssemblyBuilderAccess AssemblyBuilderAccess AssemblyBuilderAccess AssemblyBuilderAccess

動的アセンブリにアクセスするときのモード。The mode in which the dynamic assembly will be accessed.

dir
String String String String

動的アセンブリを保存するディレクトリの名前。The name of the directory where the dynamic assembly will be saved. dirnull の場合、現在のディレクトリが使用されます。If dir is null, the current directory is used.

isSynchronized
Boolean Boolean Boolean Boolean

動的アセンブリ内のモジュール、型、およびメンバーの作成を同期する場合は true。それ以外の場合は falsetrue to synchronize the creation of modules, types, and members in the dynamic assembly; otherwise, false.

assemblyAttributes
IEnumerable<CustomAttributeBuilder>

アセンブリに適用される属性を列挙した一覧。属性がない場合は nullAn enumerable list of attributes to be applied to the assembly, or null if there are no attributes.

戻り値

名前と機能が指定された動的アセンブリ。A dynamic assembly with the specified name and features.

例外

nameName プロパティが null です。The Name property of name is null.

または-or- nameName プロパティが空白で始まっているか、スラッシュまたは円記号が含まれています。The Name property of name starts with white space, or contains a forward or backward slash.

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

注釈

このメソッドのオーバー ロードを使用すると、動的アセンブリの作成時に適用しない限り、正しく動作していない属性を指定できます。Use this method overload to specify attributes that do not work correctly unless they are applied when a dynamic assembly is created. たとえば、セキュリティなどの属性SecurityTransparentAttributeSecurityCriticalAttribute動的アセンブリが作成された後に追加されている場合に正常に機能しません。For example, security attributes such as SecurityTransparentAttribute and SecurityCriticalAttribute do not work correctly if they are added after a dynamic assembly has been created.

場合isSynchronizedtrue、結果の次のメソッドAssemblyBuilderを同期する: DefineDynamicModuleDefineResourceAddResourceFileGetDynamicModuleSetEntryPoint、およびSaveします。If isSynchronized is true, the following methods of the resulting AssemblyBuilder will be synchronized: DefineDynamicModule, DefineResource, AddResourceFile, GetDynamicModule, SetEntryPoint, and Save. これらのメソッドの 2 つは、異なるスレッドで呼び出されると、もう一方が完了するまで 1 つブロックされます。If two of these methods are called on different threads, one will block until the other is completed.

こちらもご覧ください

DefineDynamicAssembly(AssemblyName, AssemblyBuilderAccess, PermissionSet, PermissionSet, PermissionSet) DefineDynamicAssembly(AssemblyName, AssemblyBuilderAccess, PermissionSet, PermissionSet, PermissionSet) DefineDynamicAssembly(AssemblyName, AssemblyBuilderAccess, PermissionSet, PermissionSet, PermissionSet) DefineDynamicAssembly(AssemblyName, AssemblyBuilderAccess, PermissionSet, PermissionSet, PermissionSet)

警告

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

名前、アクセス モード、およびアクセス許可要求を指定して、動的アセンブリを定義します。Defines a dynamic assembly using the specified name, access mode, and permission requests.

public:
 virtual System::Reflection::Emit::AssemblyBuilder ^ DefineDynamicAssembly(System::Reflection::AssemblyName ^ name, System::Reflection::Emit::AssemblyBuilderAccess access, System::Security::PermissionSet ^ requiredPermissions, System::Security::PermissionSet ^ optionalPermissions, System::Security::PermissionSet ^ refusedPermissions);
[System.Obsolete("Declarative security for assembly level is no longer enforced")]
[System.Obsolete("Assembly level declarative security is obsolete and is no longer enforced by the CLR by default.  See http://go.microsoft.com/fwlink/?LinkID=155570 for more information.")]
public System.Reflection.Emit.AssemblyBuilder DefineDynamicAssembly (System.Reflection.AssemblyName name, System.Reflection.Emit.AssemblyBuilderAccess access, System.Security.PermissionSet requiredPermissions, System.Security.PermissionSet optionalPermissions, System.Security.PermissionSet refusedPermissions);
abstract member DefineDynamicAssembly : System.Reflection.AssemblyName * System.Reflection.Emit.AssemblyBuilderAccess * System.Security.PermissionSet * System.Security.PermissionSet * System.Security.PermissionSet -> System.Reflection.Emit.AssemblyBuilder
override this.DefineDynamicAssembly : System.Reflection.AssemblyName * System.Reflection.Emit.AssemblyBuilderAccess * System.Security.PermissionSet * System.Security.PermissionSet * System.Security.PermissionSet -> System.Reflection.Emit.AssemblyBuilder
Public Function DefineDynamicAssembly (name As AssemblyName, access As AssemblyBuilderAccess, requiredPermissions As PermissionSet, optionalPermissions As PermissionSet, refusedPermissions As PermissionSet) As AssemblyBuilder

パラメーター

name
AssemblyName AssemblyName AssemblyName AssemblyName

動的アセンブリの一意の ID。The unique identity of the dynamic assembly.

access
AssemblyBuilderAccess AssemblyBuilderAccess AssemblyBuilderAccess AssemblyBuilderAccess

動的アセンブリにアクセスするときのモード。The mode in which the dynamic assembly will be accessed.

requiredPermissions
PermissionSet PermissionSet PermissionSet PermissionSet

必須のアクセス許可要求。The required permissions request.

optionalPermissions
PermissionSet PermissionSet PermissionSet PermissionSet

オプションのアクセス許可要求。The optional permissions request.

refusedPermissions
PermissionSet PermissionSet PermissionSet PermissionSet

拒否されたアクセス許可要求。The refused permissions request.

戻り値

名前と機能が指定された動的アセンブリ。A dynamic assembly with the specified name and features.

実装

例外

nameName プロパティが null です。The Name property of name is null.

または-or- nameName プロパティが空白文字で始まるか、スラッシュまたはバック スラッシュを含んでいます。The Name property of name begins with white space, or contains a forward or backward slash.

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

次のサンプルでは、DefineDynamicAssemblyメソッドとAssemblyResolveイベント。The following sample demonstrates the DefineDynamicAssembly method and AssemblyResolve event.

このコード例を実行するには、完全修飾アセンブリ名を指定する必要があります。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::Reflection;
using namespace System::Reflection::Emit;
ref class Test
{
public:
   static void InstantiateMyDynamicType( AppDomain^ domain )
   {
      try
      {
         
         // You must supply a valid fully qualified assembly name here.
         domain->CreateInstance( "Assembly text name, Version, Culture, PublicKeyToken", "MyDynamicType" );
      }
      catch ( Exception^ e ) 
      {
         Console::WriteLine( e->Message );
      }

   }

   static Assembly^ MyResolveEventHandler( Object^ sender, ResolveEventArgs^ args )
   {
      return DefineDynamicAssembly( dynamic_cast<AppDomain^>(sender) );
   }

   static Assembly^ DefineDynamicAssembly( AppDomain^ domain )
   {
      
      // Build a dynamic assembly using Reflection Emit API.
      AssemblyName^ assemblyName = gcnew AssemblyName;
      assemblyName->Name = "MyDynamicAssembly";
      AssemblyBuilder^ assemblyBuilder = domain->DefineDynamicAssembly( assemblyName, AssemblyBuilderAccess::Run );
      ModuleBuilder^ moduleBuilder = assemblyBuilder->DefineDynamicModule( "MyDynamicModule" );
      TypeBuilder^ typeBuilder = moduleBuilder->DefineType( "MyDynamicType", TypeAttributes::Public );
      ConstructorBuilder^ constructorBuilder = typeBuilder->DefineConstructor( MethodAttributes::Public, CallingConventions::Standard, nullptr );
      ILGenerator^ ilGenerator = constructorBuilder->GetILGenerator();
      ilGenerator->EmitWriteLine( "MyDynamicType instantiated!" );
      ilGenerator->Emit( OpCodes::Ret );
      typeBuilder->CreateType();
      return assemblyBuilder;
   }

};

int main()
{
   AppDomain^ currentDomain = AppDomain::CurrentDomain;
   Test::InstantiateMyDynamicType( currentDomain ); // Failed!
   currentDomain->AssemblyResolve += gcnew ResolveEventHandler( Test::MyResolveEventHandler );
   Test::InstantiateMyDynamicType( currentDomain ); // OK!
}

using System;
using System.Reflection;
using System.Reflection.Emit;

class Test {
   public static void Main() {
      AppDomain currentDomain = AppDomain.CurrentDomain;

      InstantiateMyDynamicType(currentDomain);   // Failed!
      
      currentDomain.AssemblyResolve += new ResolveEventHandler(MyResolveEventHandler);
      
      InstantiateMyDynamicType(currentDomain);   // OK!
   }

   static void InstantiateMyDynamicType(AppDomain domain) {
      try {
         // You must supply a valid fully qualified assembly name here. 
         domain.CreateInstance("Assembly text name, Version, Culture, PublicKeyToken", "MyDynamicType");
      } catch (Exception e) {
         Console.WriteLine(e.Message);
      }
   }   

   static Assembly MyResolveEventHandler(object sender, ResolveEventArgs args) {
      return DefineDynamicAssembly((AppDomain) sender);
   }
   
   static Assembly DefineDynamicAssembly(AppDomain domain) {
      // Build a dynamic assembly using Reflection Emit API.
   
      AssemblyName assemblyName = new AssemblyName();
      assemblyName.Name = "MyDynamicAssembly";

      AssemblyBuilder assemblyBuilder = domain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run);
      ModuleBuilder moduleBuilder = assemblyBuilder.DefineDynamicModule("MyDynamicModule");
      TypeBuilder typeBuilder = moduleBuilder.DefineType("MyDynamicType", TypeAttributes.Public);
      ConstructorBuilder constructorBuilder = typeBuilder.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, null);
      ILGenerator ilGenerator = constructorBuilder.GetILGenerator();
      
      ilGenerator.EmitWriteLine("MyDynamicType instantiated!");
      ilGenerator.Emit(OpCodes.Ret);

      typeBuilder.CreateType();

      return assemblyBuilder;
   }
}

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

Module Test
   
   Sub Main()
      Dim currentDomain As AppDomain = AppDomain.CurrentDomain
      
      InstantiateMyDynamicType(currentDomain)   'Failed!
      
      AddHandler currentDomain.AssemblyResolve, AddressOf MyResolveEventHandler
      
      InstantiateMyDynamicType(currentDomain)   'OK!
   End Sub 'Main
   
   Sub InstantiateMyDynamicType(domain As AppDomain)
      Try
         ' You must supply a valid fully qualified assembly name here.
         domain.CreateInstance("Assembly text name, Version, Culture, PublicKeyToken", "MyDynamicType")
      Catch e As Exception
         Console.WriteLine(e.Message)
      End Try
   End Sub 'InstantiateMyDynamicType
   
   Function MyResolveEventHandler(sender As Object, args As ResolveEventArgs) As System.Reflection.Assembly
      Return DefineDynamicAssembly(DirectCast(sender, AppDomain))
   End Function 'MyResolveEventHandler
   
   Function DefineDynamicAssembly(domain As AppDomain) As System.Reflection.Assembly
      ' Build a dynamic assembly using Reflection Emit API.

      Dim assemblyName As New AssemblyName()
      assemblyName.Name = "MyDynamicAssembly"
      
      Dim assemblyBuilder As AssemblyBuilder = domain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run)
      Dim moduleBuilder As ModuleBuilder = assemblyBuilder.DefineDynamicModule("MyDynamicModule")
      Dim typeBuilder As TypeBuilder = moduleBuilder.DefineType("MyDynamicType", TypeAttributes.Public)
      Dim constructorBuilder As ConstructorBuilder = typeBuilder.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, Nothing)
      Dim ilGenerator As ILGenerator = constructorBuilder.GetILGenerator()
      
      ilGenerator.EmitWriteLine("MyDynamicType instantiated!")
      ilGenerator.Emit(OpCodes.Ret)
      
      typeBuilder.CreateType()
      
      Return assemblyBuilder
   End Function 'DefineDynamicAssembly

End Module 'Test 

注釈

指定されたアクセス許可要求requiredPermissionsoptionalPermissions、およびrefusedPermissions動的アセンブリに保存され、メモリに再読み込みしない限りは使用されません。The permission requests specified for requiredPermissions, optionalPermissions, and refusedPermissions are not used unless the dynamic assembly has been saved and reloaded into memory. ディスクのオーバー ロードを使用することはありません保存されている一時アセンブリのアクセス許可要求を指定する、DefineDynamicAssemblyメソッドも、要求されたアクセス許可、および供給としての証拠を示す、Evidenceオブジェクト。To specify permission requests for a transient assembly that is never saved to disk, use an overload of the DefineDynamicAssembly method that specifies evidence as well as requested permissions, and supply an Evidence object.

注意

動的アセンブリを生成するコードの開発中はお勧めのオーバー ロードを使用すること、DefineDynamicAssembly証拠、アクセス許可を指定するメソッドを含む動的アセンブリを作成証拠を指定するSecurityPermissionFlag.SkipVerificationrefusedPermissions.During the development of code that emits dynamic assemblies, it is recommended that you use an overload of the DefineDynamicAssembly method that specifies evidence and permissions, supply the evidence you want the dynamic assembly to have, and include SecurityPermissionFlag.SkipVerification in refusedPermissions. 含むSkipVerificationで、refusedPermissionsパラメーターは、MSIL が検証されていることを保証します。Including SkipVerification in the refusedPermissions parameter ensures that the MSIL is verified. この技法の制限はまた、あるSecurityException完全な信頼を要求するコードで使用する場合にスローされます。A limitation of this technique is that it also causes SecurityException to be thrown when used with code that demands full trust.

このメソッドは、現在のアプリケーション ドメインで、動的アセンブリを定義するのみ使用する必要があります。This method should only be used to define a dynamic assembly in the current application domain. 詳細については、次を参照してください。、Load(AssemblyName)メソッドのオーバー ロードします。For more information, see the Load(AssemblyName) method overload .

DefineDynamicAssembly(AssemblyName, AssemblyBuilderAccess, String, Evidence) DefineDynamicAssembly(AssemblyName, AssemblyBuilderAccess, String, Evidence) DefineDynamicAssembly(AssemblyName, AssemblyBuilderAccess, String, Evidence)

警告

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

名前、アクセス モード、ストレージ ディレクトリ、および証拠を指定して、動的アセンブリを定義します。Defines a dynamic assembly using the specified name, access mode, storage directory, and evidence.

public:
 virtual System::Reflection::Emit::AssemblyBuilder ^ DefineDynamicAssembly(System::Reflection::AssemblyName ^ name, System::Reflection::Emit::AssemblyBuilderAccess access, System::String ^ dir, System::Security::Policy::Evidence ^ evidence);
[System.Obsolete("Declarative security for assembly level is no longer enforced")]
[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 DefineDynamicAssembly which does not take an Evidence parameter. See http://go.microsoft.com/fwlink/?LinkId=155570 for more information.")]
public System.Reflection.Emit.AssemblyBuilder DefineDynamicAssembly (System.Reflection.AssemblyName name, System.Reflection.Emit.AssemblyBuilderAccess access, string dir, System.Security.Policy.Evidence evidence);
abstract member DefineDynamicAssembly : System.Reflection.AssemblyName * System.Reflection.Emit.AssemblyBuilderAccess * string * System.Security.Policy.Evidence -> System.Reflection.Emit.AssemblyBuilder
override this.DefineDynamicAssembly : System.Reflection.AssemblyName * System.Reflection.Emit.AssemblyBuilderAccess * string * System.Security.Policy.Evidence -> System.Reflection.Emit.AssemblyBuilder

パラメーター

name
AssemblyName AssemblyName AssemblyName AssemblyName

動的アセンブリの一意の ID。The unique identity of the dynamic assembly.

access
AssemblyBuilderAccess AssemblyBuilderAccess AssemblyBuilderAccess AssemblyBuilderAccess

動的アセンブリにアクセスするときのモード。The mode in which the dynamic assembly will be accessed.

dir
String String String String

アセンブリを保存するディレクトリの名前。The name of the directory where the assembly will be saved. dirnull の場合は、現在のディレクトリが既定で使用されます。If dir is null, the directory defaults to the current directory.

evidence
Evidence Evidence Evidence Evidence

動的アセンブリに指定する証拠。The evidence supplied for the dynamic assembly. 証拠は、ポリシーの解決に使用する証拠の最終セットを変更されないために使用されます。The evidence is used unaltered as the final set of evidence used for policy resolution.

戻り値

名前と機能が指定された動的アセンブリ。A dynamic assembly with the specified name and features.

実装

例外

nameName プロパティが null です。The Name property of name is null.

または-or- nameName プロパティが空白文字で始まるか、スラッシュまたはバック スラッシュを含んでいます。The Name property of name begins with white space, or contains a forward or backward slash.

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

次のサンプルでは、DefineDynamicAssemblyメソッドとAssemblyResolveイベント。The following sample demonstrates the DefineDynamicAssembly method and AssemblyResolve event.

このコード例を実行するには、完全修飾アセンブリ名を指定する必要があります。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::Reflection;
using namespace System::Reflection::Emit;
ref class Test
{
public:
   static void InstantiateMyDynamicType( AppDomain^ domain )
   {
      try
      {
         
         // You must supply a valid fully qualified assembly name here.
         domain->CreateInstance( "Assembly text name, Version, Culture, PublicKeyToken", "MyDynamicType" );
      }
      catch ( Exception^ e ) 
      {
         Console::WriteLine( e->Message );
      }

   }

   static Assembly^ MyResolveEventHandler( Object^ sender, ResolveEventArgs^ args )
   {
      return DefineDynamicAssembly( dynamic_cast<AppDomain^>(sender) );
   }

   static Assembly^ DefineDynamicAssembly( AppDomain^ domain )
   {
      
      // Build a dynamic assembly using Reflection Emit API.
      AssemblyName^ assemblyName = gcnew AssemblyName;
      assemblyName->Name = "MyDynamicAssembly";
      AssemblyBuilder^ assemblyBuilder = domain->DefineDynamicAssembly( assemblyName, AssemblyBuilderAccess::Run );
      ModuleBuilder^ moduleBuilder = assemblyBuilder->DefineDynamicModule( "MyDynamicModule" );
      TypeBuilder^ typeBuilder = moduleBuilder->DefineType( "MyDynamicType", TypeAttributes::Public );
      ConstructorBuilder^ constructorBuilder = typeBuilder->DefineConstructor( MethodAttributes::Public, CallingConventions::Standard, nullptr );
      ILGenerator^ ilGenerator = constructorBuilder->GetILGenerator();
      ilGenerator->EmitWriteLine( "MyDynamicType instantiated!" );
      ilGenerator->Emit( OpCodes::Ret );
      typeBuilder->CreateType();
      return assemblyBuilder;
   }

};

int main()
{
   AppDomain^ currentDomain = AppDomain::CurrentDomain;
   Test::InstantiateMyDynamicType( currentDomain ); // Failed!
   currentDomain->AssemblyResolve += gcnew ResolveEventHandler( Test::MyResolveEventHandler );
   Test::InstantiateMyDynamicType( currentDomain ); // OK!
}

using System;
using System.Reflection;
using System.Reflection.Emit;

class Test {
   public static void Main() {
      AppDomain currentDomain = AppDomain.CurrentDomain;

      InstantiateMyDynamicType(currentDomain);   // Failed!
      
      currentDomain.AssemblyResolve += new ResolveEventHandler(MyResolveEventHandler);
      
      InstantiateMyDynamicType(currentDomain);   // OK!
   }

   static void InstantiateMyDynamicType(AppDomain domain) {
      try {
         // You must supply a valid fully qualified assembly name here. 
         domain.CreateInstance("Assembly text name, Version, Culture, PublicKeyToken", "MyDynamicType");
      } catch (Exception e) {
         Console.WriteLine(e.Message);
      }
   }   

   static Assembly MyResolveEventHandler(object sender, ResolveEventArgs args) {
      return DefineDynamicAssembly((AppDomain) sender);
   }
   
   static Assembly DefineDynamicAssembly(AppDomain domain) {
      // Build a dynamic assembly using Reflection Emit API.
   
      AssemblyName assemblyName = new AssemblyName();
      assemblyName.Name = "MyDynamicAssembly";

      AssemblyBuilder assemblyBuilder = domain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run);
      ModuleBuilder moduleBuilder = assemblyBuilder.DefineDynamicModule("MyDynamicModule");
      TypeBuilder typeBuilder = moduleBuilder.DefineType("MyDynamicType", TypeAttributes.Public);
      ConstructorBuilder constructorBuilder = typeBuilder.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, null);
      ILGenerator ilGenerator = constructorBuilder.GetILGenerator();
      
      ilGenerator.EmitWriteLine("MyDynamicType instantiated!");
      ilGenerator.Emit(OpCodes.Ret);

      typeBuilder.CreateType();

      return assemblyBuilder;
   }
}

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

Module Test
   
   Sub Main()
      Dim currentDomain As AppDomain = AppDomain.CurrentDomain
      
      InstantiateMyDynamicType(currentDomain)   'Failed!
      
      AddHandler currentDomain.AssemblyResolve, AddressOf MyResolveEventHandler
      
      InstantiateMyDynamicType(currentDomain)   'OK!
   End Sub 'Main
   
   Sub InstantiateMyDynamicType(domain As AppDomain)
      Try
         ' You must supply a valid fully qualified assembly name here.
         domain.CreateInstance("Assembly text name, Version, Culture, PublicKeyToken", "MyDynamicType")
      Catch e As Exception
         Console.WriteLine(e.Message)
      End Try
   End Sub 'InstantiateMyDynamicType
   
   Function MyResolveEventHandler(sender As Object, args As ResolveEventArgs) As System.Reflection.Assembly
      Return DefineDynamicAssembly(DirectCast(sender, AppDomain))
   End Function 'MyResolveEventHandler
   
   Function DefineDynamicAssembly(domain As AppDomain) As System.Reflection.Assembly
      ' Build a dynamic assembly using Reflection Emit API.

      Dim assemblyName As New AssemblyName()
      assemblyName.Name = "MyDynamicAssembly"
      
      Dim assemblyBuilder As AssemblyBuilder = domain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run)
      Dim moduleBuilder As ModuleBuilder = assemblyBuilder.DefineDynamicModule("MyDynamicModule")
      Dim typeBuilder As TypeBuilder = moduleBuilder.DefineType("MyDynamicType", TypeAttributes.Public)
      Dim constructorBuilder As ConstructorBuilder = typeBuilder.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, Nothing)
      Dim ilGenerator As ILGenerator = constructorBuilder.GetILGenerator()
      
      ilGenerator.EmitWriteLine("MyDynamicType instantiated!")
      ilGenerator.Emit(OpCodes.Ret)
      
      typeBuilder.CreateType()
      
      Return assemblyBuilder
   End Function 'DefineDynamicAssembly

End Module 'Test 

注釈

のみ完全に信頼された呼び出し元を指定できます、evidence動的を定義するときにAssemblyします。Only fully trusted callers can supply their evidence when defining a dynamic Assembly. マップは、ランタイム、Evidence付与されたアクセス許可を決定するセキュリティ ポリシーを使用します。The runtime will map the Evidence through the security policy to determine the granted permissions. 呼び出し元は、null を指定する必要がありますを部分的に信頼されたevidenceします。Partially trusted callers must supply a null evidence. 場合evidencenull、ランタイムによってアクセス許可セットがコピー、現在の付与とから呼び出し元のセットを拒否Assemblyを動的Assembly定義されていると、ポリシーを解決済みとしてマークします。If evidence is null, the runtime copies the permission sets, that is, the current grant and deny sets, from the caller's Assembly to the dynamic Assembly being defined and marks policy as resolved.

場合、動的Assemblyがディスクに保存すると、その後の読み込みは、場所に関連付けられたポリシーに基づいて付与を取得は場所、Assemblyが保存されました。If the dynamic Assembly is saved to disk, subsequent loads will get grants based on policies associated with the location where the Assembly was saved.

このメソッドは、現在のアプリケーション ドメインで、動的アセンブリを定義するのみ使用する必要があります。This method should only be used to define a dynamic assembly in the current application domain. 詳細については、次を参照してください。、Load(AssemblyName)メソッドのオーバー ロードします。For more information, see the Load(AssemblyName) method overload.

注意

動的アセンブリを生成するコードの開発中はお勧めのオーバー ロードを使用すること、DefineDynamicAssembly証拠、アクセス許可を指定するメソッドを含む動的アセンブリを作成証拠を指定するSecurityPermissionFlag.SkipVerificationrefusedPermissions.During the development of code that emits dynamic assemblies, it is recommended that you use an overload of the DefineDynamicAssembly method that specifies evidence and permissions, supply the evidence you want the dynamic assembly to have, and include SecurityPermissionFlag.SkipVerification in refusedPermissions. 含むSkipVerificationで、refusedPermissionsパラメーターは、MSIL が検証されていることを保証します。Including SkipVerification in the refusedPermissions parameter ensures that the MSIL is verified. この技法の制限はまた、あるSecurityException完全な信頼を要求するコードで使用する場合にスローされます。A limitation of this technique is that it also causes SecurityException to be thrown when used with code that demands full trust.

セキュリティ

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

DefineDynamicAssembly(AssemblyName, AssemblyBuilderAccess, IEnumerable<CustomAttributeBuilder>, SecurityContextSource) DefineDynamicAssembly(AssemblyName, AssemblyBuilderAccess, IEnumerable<CustomAttributeBuilder>, SecurityContextSource) DefineDynamicAssembly(AssemblyName, AssemblyBuilderAccess, IEnumerable<CustomAttributeBuilder>, SecurityContextSource)

名前、アクセス モード、カスタム属性、およびセキュリティ コンテキストのソースを指定して、動的アセンブリを定義します。Defines a dynamic assembly with the specified name, access mode, and custom attributes, and using the specified source for its security context.

public:
 System::Reflection::Emit::AssemblyBuilder ^ DefineDynamicAssembly(System::Reflection::AssemblyName ^ name, System::Reflection::Emit::AssemblyBuilderAccess access, System::Collections::Generic::IEnumerable<System::Reflection::Emit::CustomAttributeBuilder ^> ^ assemblyAttributes, System::Security::SecurityContextSource securityContextSource);
public System.Reflection.Emit.AssemblyBuilder DefineDynamicAssembly (System.Reflection.AssemblyName name, System.Reflection.Emit.AssemblyBuilderAccess access, System.Collections.Generic.IEnumerable<System.Reflection.Emit.CustomAttributeBuilder> assemblyAttributes, System.Security.SecurityContextSource securityContextSource);
member this.DefineDynamicAssembly : System.Reflection.AssemblyName * System.Reflection.Emit.AssemblyBuilderAccess * seq<System.Reflection.Emit.CustomAttributeBuilder> * System.Security.SecurityContextSource -> System.Reflection.Emit.AssemblyBuilder

パラメーター

name
AssemblyName AssemblyName AssemblyName AssemblyName

動的アセンブリの一意の ID。The unique identity of the dynamic assembly.

access
AssemblyBuilderAccess AssemblyBuilderAccess AssemblyBuilderAccess AssemblyBuilderAccess

動的アセンブリのアクセス モード。The access mode for the dynamic assembly.

assemblyAttributes
IEnumerable<CustomAttributeBuilder>

アセンブリに適用される属性を列挙した一覧。属性がない場合は nullAn enumerable list of attributes to be applied to the assembly, or null if there are no attributes.

securityContextSource
SecurityContextSource SecurityContextSource SecurityContextSource SecurityContextSource

セキュリティ コンテキストのソース。The source of the security context.

戻り値

名前と機能が指定された動的アセンブリ。A dynamic assembly with the specified name and features.

例外

nameName プロパティが null です。The Name property of name is null.

または-or- nameName プロパティが空白で始まっているか、スラッシュまたは円記号が含まれています。The Name property of name starts with white space, or contains a forward or backward slash.

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

securityContextSource の値が列挙値ではありませんでした。The value of securityContextSource was not one of the enumeration values.

注釈

このメソッドのオーバー ロードを使用すると、動的アセンブリの作成時に適用しない限り、正しく動作していない属性を指定できます。Use this method overload to specify attributes that do not work correctly unless they are applied when a dynamic assembly is created. たとえば、セキュリティなどの属性SecurityTransparentAttributeSecurityCriticalAttribute動的アセンブリが作成された後に追加されている場合に正常に機能しません。For example, security attributes such as SecurityTransparentAttribute and SecurityCriticalAttribute do not work correctly if they are added after a dynamic assembly has been created.

このメソッドは、現在のアプリケーション ドメインで、動的アセンブリを定義するときだけ使用する必要があります。This method should be used only to define a dynamic assembly in the current application domain. この制限の詳細については、次を参照してください。、Load(AssemblyName)メソッドのオーバー ロードします。For more information about this restriction, see the Load(AssemblyName) method overload.

DefineDynamicAssembly(AssemblyName, AssemblyBuilderAccess, String) DefineDynamicAssembly(AssemblyName, AssemblyBuilderAccess, String) DefineDynamicAssembly(AssemblyName, AssemblyBuilderAccess, String) DefineDynamicAssembly(AssemblyName, AssemblyBuilderAccess, String)

名前、アクセス モード、およびストレージ ディレクトリを指定して、動的アセンブリを定義します。Defines a dynamic assembly using the specified name, access mode, and storage directory.

public:
 virtual System::Reflection::Emit::AssemblyBuilder ^ DefineDynamicAssembly(System::Reflection::AssemblyName ^ name, System::Reflection::Emit::AssemblyBuilderAccess access, System::String ^ dir);
public System.Reflection.Emit.AssemblyBuilder DefineDynamicAssembly (System.Reflection.AssemblyName name, System.Reflection.Emit.AssemblyBuilderAccess access, string dir);
abstract member DefineDynamicAssembly : System.Reflection.AssemblyName * System.Reflection.Emit.AssemblyBuilderAccess * string -> System.Reflection.Emit.AssemblyBuilder
override this.DefineDynamicAssembly : System.Reflection.AssemblyName * System.Reflection.Emit.AssemblyBuilderAccess * string -> System.Reflection.Emit.AssemblyBuilder
Public Function DefineDynamicAssembly (name As AssemblyName, access As AssemblyBuilderAccess, dir As String) As AssemblyBuilder

パラメーター

name
AssemblyName AssemblyName AssemblyName AssemblyName

動的アセンブリの一意の ID。The unique identity of the dynamic assembly.

access
AssemblyBuilderAccess AssemblyBuilderAccess AssemblyBuilderAccess AssemblyBuilderAccess

動的アセンブリにアクセスするときのモード。The mode in which the dynamic assembly will be accessed.

dir
String String String String

アセンブリを保存するディレクトリの名前。The name of the directory where the assembly will be saved. dirnull の場合は、現在のディレクトリが既定で使用されます。If dir is null, the directory defaults to the current directory.

戻り値

名前と機能が指定された動的アセンブリ。A dynamic assembly with the specified name and features.

実装

例外

nameName プロパティが null です。The Name property of name is null.

または-or- nameName プロパティが空白文字で始まるか、スラッシュまたはバック スラッシュを含んでいます。The Name property of name begins with white space, or contains a forward or backward slash.

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

次のサンプルでは、DefineDynamicAssemblyメソッドとAssemblyResolveイベント。The following sample demonstrates the DefineDynamicAssembly method and AssemblyResolve event.

このコード例を実行するには、完全修飾アセンブリ名を指定する必要があります。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::Reflection;
using namespace System::Reflection::Emit;
ref class Test
{
public:
   static void InstantiateMyDynamicType( AppDomain^ domain )
   {
      try
      {
         
         // You must supply a valid fully qualified assembly name here.
         domain->CreateInstance( "Assembly text name, Version, Culture, PublicKeyToken", "MyDynamicType" );
      }
      catch ( Exception^ e ) 
      {
         Console::WriteLine( e->Message );
      }

   }

   static Assembly^ MyResolveEventHandler( Object^ sender, ResolveEventArgs^ args )
   {
      return DefineDynamicAssembly( dynamic_cast<AppDomain^>(sender) );
   }

   static Assembly^ DefineDynamicAssembly( AppDomain^ domain )
   {
      
      // Build a dynamic assembly using Reflection Emit API.
      AssemblyName^ assemblyName = gcnew AssemblyName;
      assemblyName->Name = "MyDynamicAssembly";
      AssemblyBuilder^ assemblyBuilder = domain->DefineDynamicAssembly( assemblyName, AssemblyBuilderAccess::Run );
      ModuleBuilder^ moduleBuilder = assemblyBuilder->DefineDynamicModule( "MyDynamicModule" );
      TypeBuilder^ typeBuilder = moduleBuilder->DefineType( "MyDynamicType", TypeAttributes::Public );
      ConstructorBuilder^ constructorBuilder = typeBuilder->DefineConstructor( MethodAttributes::Public, CallingConventions::Standard, nullptr );
      ILGenerator^ ilGenerator = constructorBuilder->GetILGenerator();
      ilGenerator->EmitWriteLine( "MyDynamicType instantiated!" );
      ilGenerator->Emit( OpCodes::Ret );
      typeBuilder->CreateType();
      return assemblyBuilder;
   }

};

int main()
{
   AppDomain^ currentDomain = AppDomain::CurrentDomain;
   Test::InstantiateMyDynamicType( currentDomain ); // Failed!
   currentDomain->AssemblyResolve += gcnew ResolveEventHandler( Test::MyResolveEventHandler );
   Test::InstantiateMyDynamicType( currentDomain ); // OK!
}

using System;
using System.Reflection;
using System.Reflection.Emit;

class Test {
   public static void Main() {
      AppDomain currentDomain = AppDomain.CurrentDomain;

      InstantiateMyDynamicType(currentDomain);   // Failed!
      
      currentDomain.AssemblyResolve += new ResolveEventHandler(MyResolveEventHandler);
      
      InstantiateMyDynamicType(currentDomain);   // OK!
   }

   static void InstantiateMyDynamicType(AppDomain domain) {
      try {
         // You must supply a valid fully qualified assembly name here. 
         domain.CreateInstance("Assembly text name, Version, Culture, PublicKeyToken", "MyDynamicType");
      } catch (Exception e) {
         Console.WriteLine(e.Message);
      }
   }   

   static Assembly MyResolveEventHandler(object sender, ResolveEventArgs args) {
      return DefineDynamicAssembly((AppDomain) sender);
   }
   
   static Assembly DefineDynamicAssembly(AppDomain domain) {
      // Build a dynamic assembly using Reflection Emit API.
   
      AssemblyName assemblyName = new AssemblyName();
      assemblyName.Name = "MyDynamicAssembly";

      AssemblyBuilder assemblyBuilder = domain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run);
      ModuleBuilder moduleBuilder = assemblyBuilder.DefineDynamicModule("MyDynamicModule");
      TypeBuilder typeBuilder = moduleBuilder.DefineType("MyDynamicType", TypeAttributes.Public);
      ConstructorBuilder constructorBuilder = typeBuilder.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, null);
      ILGenerator ilGenerator = constructorBuilder.GetILGenerator();
      
      ilGenerator.EmitWriteLine("MyDynamicType instantiated!");
      ilGenerator.Emit(OpCodes.Ret);

      typeBuilder.CreateType();

      return assemblyBuilder;
   }
}

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

Module Test
   
   Sub Main()
      Dim currentDomain As AppDomain = AppDomain.CurrentDomain
      
      InstantiateMyDynamicType(currentDomain)   'Failed!
      
      AddHandler currentDomain.AssemblyResolve, AddressOf MyResolveEventHandler
      
      InstantiateMyDynamicType(currentDomain)   'OK!
   End Sub 'Main
   
   Sub InstantiateMyDynamicType(domain As AppDomain)
      Try
         ' You must supply a valid fully qualified assembly name here.
         domain.CreateInstance("Assembly text name, Version, Culture, PublicKeyToken", "MyDynamicType")
      Catch e As Exception
         Console.WriteLine(e.Message)
      End Try
   End Sub 'InstantiateMyDynamicType
   
   Function MyResolveEventHandler(sender As Object, args As ResolveEventArgs) As System.Reflection.Assembly
      Return DefineDynamicAssembly(DirectCast(sender, AppDomain))
   End Function 'MyResolveEventHandler
   
   Function DefineDynamicAssembly(domain As AppDomain) As System.Reflection.Assembly
      ' Build a dynamic assembly using Reflection Emit API.

      Dim assemblyName As New AssemblyName()
      assemblyName.Name = "MyDynamicAssembly"
      
      Dim assemblyBuilder As AssemblyBuilder = domain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run)
      Dim moduleBuilder As ModuleBuilder = assemblyBuilder.DefineDynamicModule("MyDynamicModule")
      Dim typeBuilder As TypeBuilder = moduleBuilder.DefineType("MyDynamicType", TypeAttributes.Public)
      Dim constructorBuilder As ConstructorBuilder = typeBuilder.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, Nothing)
      Dim ilGenerator As ILGenerator = constructorBuilder.GetILGenerator()
      
      ilGenerator.EmitWriteLine("MyDynamicType instantiated!")
      ilGenerator.Emit(OpCodes.Ret)
      
      typeBuilder.CreateType()
      
      Return assemblyBuilder
   End Function 'DefineDynamicAssembly

End Module 'Test 

注釈

このメソッドは、現在のアプリケーション ドメインで、動的アセンブリを定義するのみ使用する必要があります。This method should only be used to define a dynamic assembly in the current application domain. 詳細については、次を参照してください。、Load(AssemblyName)メソッドのオーバー ロードします。For more information, see the Load(AssemblyName) method overload.

注意

動的アセンブリを生成するコードの開発中はお勧めのオーバー ロードを使用すること、DefineDynamicAssembly証拠、アクセス許可を指定するメソッドを含む動的アセンブリを作成証拠を指定するSecurityPermissionFlag.SkipVerificationrefusedPermissions.During the development of code that emits dynamic assemblies, it is recommended that you use an overload of the DefineDynamicAssembly method that specifies evidence and permissions, supply the evidence you want the dynamic assembly to have, and include SecurityPermissionFlag.SkipVerification in refusedPermissions. 含むSkipVerificationで、refusedPermissionsパラメーターは、MSIL が検証されていることを保証します。Including SkipVerification in the refusedPermissions parameter ensures that the MSIL is verified. この技法の制限はまた、あるSecurityException完全な信頼を要求するコードで使用する場合にスローされます。A limitation of this technique is that it also causes SecurityException to be thrown when used with code that demands full trust.

DefineDynamicAssembly(AssemblyName, AssemblyBuilderAccess, Evidence) DefineDynamicAssembly(AssemblyName, AssemblyBuilderAccess, Evidence) DefineDynamicAssembly(AssemblyName, AssemblyBuilderAccess, Evidence)

警告

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

名前、アクセス モード、および証拠を指定して、動的アセンブリを定義します。Defines a dynamic assembly using the specified name, access mode, and evidence.

public:
 virtual System::Reflection::Emit::AssemblyBuilder ^ DefineDynamicAssembly(System::Reflection::AssemblyName ^ name, System::Reflection::Emit::AssemblyBuilderAccess access, System::Security::Policy::Evidence ^ evidence);
[System.Obsolete("Declarative security for assembly level is no longer enforced")]
[System.Obsolete("Assembly level declarative security is obsolete and is no longer enforced by the CLR by default.  See http://go.microsoft.com/fwlink/?LinkID=155570 for more information.")]
public System.Reflection.Emit.AssemblyBuilder DefineDynamicAssembly (System.Reflection.AssemblyName name, System.Reflection.Emit.AssemblyBuilderAccess access, System.Security.Policy.Evidence evidence);
abstract member DefineDynamicAssembly : System.Reflection.AssemblyName * System.Reflection.Emit.AssemblyBuilderAccess * System.Security.Policy.Evidence -> System.Reflection.Emit.AssemblyBuilder
override this.DefineDynamicAssembly : System.Reflection.AssemblyName * System.Reflection.Emit.AssemblyBuilderAccess * System.Security.Policy.Evidence -> System.Reflection.Emit.AssemblyBuilder

パラメーター

name
AssemblyName AssemblyName AssemblyName AssemblyName

動的アセンブリの一意の ID。The unique identity of the dynamic assembly.

access
AssemblyBuilderAccess AssemblyBuilderAccess AssemblyBuilderAccess AssemblyBuilderAccess

動的アセンブリにアクセスするときのモード。The mode in which the dynamic assembly will be accessed.

evidence
Evidence Evidence Evidence Evidence

動的アセンブリに指定する証拠。The evidence supplied for the dynamic assembly. 証拠は、ポリシーの解決に使用する証拠の最終セットを変更されないために使用されます。The evidence is used unaltered as the final set of evidence used for policy resolution.

戻り値

名前と機能が指定された動的アセンブリ。A dynamic assembly with the specified name and features.

実装

例外

nameName プロパティが null です。The Name property of name is null.

または-or- nameName プロパティが空白文字で始まるか、スラッシュまたはバック スラッシュを含んでいます。The Name property of name begins with white space, or contains a forward or backward slash.

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

次のサンプルでは、DefineDynamicAssemblyメソッドとAssemblyResolveイベント。The following sample demonstrates the DefineDynamicAssembly method and the AssemblyResolve event.

最初に、コード例がのインスタンスを作成しようMyDynamicType呼び出すことによって、CreateInstanceメソッドは無効なアセンブリ名、および結果の例外をキャッチします。First, the code example tries to create an instance of MyDynamicType by calling the CreateInstance method with an invalid assembly name, and catches the resulting exception.

コード例のイベント ハンドラーを追加し、AssemblyResolveイベント、もう一度のインスタンスを作成しようとMyDynamicTypeします。The code example then adds an event handler for the AssemblyResolve event, and again tries to create an instance ofMyDynamicType. 呼び出し中にCreateInstanceAssemblyResolve無効なアセンブリのイベントが発生します。During the call to CreateInstance, the AssemblyResolve event is raised for the invalid assembly. イベント ハンドラーは、という名前の型を含む動的アセンブリを作成します。 MyDynamicType、型、パラメーターなしのコンス トラクターを提供し、新しい動的アセンブリを返します。The event handler creates a dynamic assembly that contains a type named MyDynamicType, gives the type a parameterless constructor, and returns the new dynamic assembly. 呼び出しCreateInstance、正常に終了した後のコンス トラクターとMyDynamicTypeコンソールにメッセージが表示されます。The call to CreateInstance then finishes successfully, and the constructor for MyDynamicType displays a message at the console.

using namespace System;
using namespace System::Reflection;
using namespace System::Reflection::Emit;
ref class Test
{
public:
   static void InstantiateMyDynamicType( AppDomain^ domain )
   {
      try
      {
         
         // You must supply a valid fully qualified assembly name here.
         domain->CreateInstance( "Assembly text name, Version, Culture, PublicKeyToken", "MyDynamicType" );
      }
      catch ( Exception^ e ) 
      {
         Console::WriteLine( e->Message );
      }

   }

   static Assembly^ MyResolveEventHandler( Object^ sender, ResolveEventArgs^ args )
   {
      return DefineDynamicAssembly( dynamic_cast<AppDomain^>(sender) );
   }

   static Assembly^ DefineDynamicAssembly( AppDomain^ domain )
   {
      
      // Build a dynamic assembly using Reflection Emit API.
      AssemblyName^ assemblyName = gcnew AssemblyName;
      assemblyName->Name = "MyDynamicAssembly";
      AssemblyBuilder^ assemblyBuilder = domain->DefineDynamicAssembly( assemblyName, AssemblyBuilderAccess::Run );
      ModuleBuilder^ moduleBuilder = assemblyBuilder->DefineDynamicModule( "MyDynamicModule" );
      TypeBuilder^ typeBuilder = moduleBuilder->DefineType( "MyDynamicType", TypeAttributes::Public );
      ConstructorBuilder^ constructorBuilder = typeBuilder->DefineConstructor( MethodAttributes::Public, CallingConventions::Standard, nullptr );
      ILGenerator^ ilGenerator = constructorBuilder->GetILGenerator();
      ilGenerator->EmitWriteLine( "MyDynamicType instantiated!" );
      ilGenerator->Emit( OpCodes::Ret );
      typeBuilder->CreateType();
      return assemblyBuilder;
   }

};

int main()
{
   AppDomain^ currentDomain = AppDomain::CurrentDomain;
   Test::InstantiateMyDynamicType( currentDomain ); // Failed!
   currentDomain->AssemblyResolve += gcnew ResolveEventHandler( Test::MyResolveEventHandler );
   Test::InstantiateMyDynamicType( currentDomain ); // OK!
}

using System;
using System.Reflection;
using System.Reflection.Emit;

class Test {
   public static void Main() {
      AppDomain currentDomain = AppDomain.CurrentDomain;

      InstantiateMyDynamicType(currentDomain);   // Failed!
      
      currentDomain.AssemblyResolve += new ResolveEventHandler(MyResolveEventHandler);
      
      InstantiateMyDynamicType(currentDomain);   // OK!
   }

   static void InstantiateMyDynamicType(AppDomain domain) {
      try {
         // You must supply a valid fully qualified assembly name here. 
         domain.CreateInstance("Assembly text name, Version, Culture, PublicKeyToken", "MyDynamicType");
      } catch (Exception e) {
         Console.WriteLine(e.Message);
      }
   }   

   static Assembly MyResolveEventHandler(object sender, ResolveEventArgs args) {
      return DefineDynamicAssembly((AppDomain) sender);
   }
   
   static Assembly DefineDynamicAssembly(AppDomain domain) {
      // Build a dynamic assembly using Reflection Emit API.
   
      AssemblyName assemblyName = new AssemblyName();
      assemblyName.Name = "MyDynamicAssembly";

      AssemblyBuilder assemblyBuilder = domain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run);
      ModuleBuilder moduleBuilder = assemblyBuilder.DefineDynamicModule("MyDynamicModule");
      TypeBuilder typeBuilder = moduleBuilder.DefineType("MyDynamicType", TypeAttributes.Public);
      ConstructorBuilder constructorBuilder = typeBuilder.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, null);
      ILGenerator ilGenerator = constructorBuilder.GetILGenerator();
      
      ilGenerator.EmitWriteLine("MyDynamicType instantiated!");
      ilGenerator.Emit(OpCodes.Ret);

      typeBuilder.CreateType();

      return assemblyBuilder;
   }
}

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

Module Test
   
   Sub Main()
      Dim currentDomain As AppDomain = AppDomain.CurrentDomain
      
      InstantiateMyDynamicType(currentDomain)   'Failed!
      
      AddHandler currentDomain.AssemblyResolve, AddressOf MyResolveEventHandler
      
      InstantiateMyDynamicType(currentDomain)   'OK!
   End Sub 'Main
   
   Sub InstantiateMyDynamicType(domain As AppDomain)
      Try
         ' You must supply a valid fully qualified assembly name here.
         domain.CreateInstance("Assembly text name, Version, Culture, PublicKeyToken", "MyDynamicType")
      Catch e As Exception
         Console.WriteLine(e.Message)
      End Try
   End Sub 'InstantiateMyDynamicType
   
   Function MyResolveEventHandler(sender As Object, args As ResolveEventArgs) As System.Reflection.Assembly
      Return DefineDynamicAssembly(DirectCast(sender, AppDomain))
   End Function 'MyResolveEventHandler
   
   Function DefineDynamicAssembly(domain As AppDomain) As System.Reflection.Assembly
      ' Build a dynamic assembly using Reflection Emit API.

      Dim assemblyName As New AssemblyName()
      assemblyName.Name = "MyDynamicAssembly"
      
      Dim assemblyBuilder As AssemblyBuilder = domain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run)
      Dim moduleBuilder As ModuleBuilder = assemblyBuilder.DefineDynamicModule("MyDynamicModule")
      Dim typeBuilder As TypeBuilder = moduleBuilder.DefineType("MyDynamicType", TypeAttributes.Public)
      Dim constructorBuilder As ConstructorBuilder = typeBuilder.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, Nothing)
      Dim ilGenerator As ILGenerator = constructorBuilder.GetILGenerator()
      
      ilGenerator.EmitWriteLine("MyDynamicType instantiated!")
      ilGenerator.Emit(OpCodes.Ret)
      
      typeBuilder.CreateType()
      
      Return assemblyBuilder
   End Function 'DefineDynamicAssembly

End Module 'Test 

注釈

のみ完全に信頼された呼び出し元を指定できます、evidence動的を定義するときにAssemblyします。Only fully trusted callers can supply their evidence when defining a dynamic Assembly. マップは、ランタイム、Evidence付与されたアクセス許可を決定するセキュリティ ポリシーを使用します。The runtime will map the Evidence through the security policy to determine the granted permissions. 呼び出し元は、null を指定する必要がありますを部分的に信頼されたevidenceします。Partially trusted callers must supply a null evidence. 場合evidencenull、ランタイムによってアクセス許可セットがコピー、現在の付与とから呼び出し元のセットを拒否Assemblyを動的Assembly定義されていると、ポリシーを解決済みとしてマークします。If evidence is null, the runtime copies the permission sets, that is, the current grant and deny sets, from the caller's Assembly to the dynamic Assembly being defined and marks policy as resolved.

場合、動的Assemblyがディスクに保存すると、その後の読み込みは、場所に関連付けられたポリシーに基づいて付与を取得は場所、Assemblyが保存されました。If the dynamic Assembly is saved to disk, subsequent loads will get grants based on policies associated with the location where the Assembly was saved.

このメソッドは、現在のアプリケーション ドメインで、動的アセンブリを定義するのみ使用する必要があります。This method should only be used to define a dynamic assembly in the current application domain. 詳細については、次を参照してください。、Load(AssemblyName)メソッドのオーバー ロードします。For more information, see the Load(AssemblyName) method overload.

注意

動的アセンブリを生成するコードの開発中はお勧めのオーバー ロードを使用すること、DefineDynamicAssembly証拠、アクセス許可を指定するメソッドを含む動的アセンブリを作成証拠を指定するSecurityPermissionFlag.SkipVerificationrefusedPermissions.During the development of code that emits dynamic assemblies, it is recommended that you use an overload of the DefineDynamicAssembly method that specifies evidence and permissions, supply the evidence you want the dynamic assembly to have, and include SecurityPermissionFlag.SkipVerification in refusedPermissions. 含むSkipVerificationで、refusedPermissionsパラメーターは、MSIL が検証されていることを保証します。Including SkipVerification in the refusedPermissions parameter ensures that the MSIL is verified. この技法の制限はまた、あるSecurityException完全な信頼を要求するコードで使用する場合にスローされます。A limitation of this technique is that it also causes SecurityException to be thrown when used with code that demands full trust.

セキュリティ

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

DefineDynamicAssembly(AssemblyName, AssemblyBuilderAccess, IEnumerable<CustomAttributeBuilder>) DefineDynamicAssembly(AssemblyName, AssemblyBuilderAccess, IEnumerable<CustomAttributeBuilder>) DefineDynamicAssembly(AssemblyName, AssemblyBuilderAccess, IEnumerable<CustomAttributeBuilder>) DefineDynamicAssembly(AssemblyName, AssemblyBuilderAccess, IEnumerable<CustomAttributeBuilder>)

名前、アクセス モード、およびカスタム属性を指定して、動的アセンブリを定義します。Defines a dynamic assembly with the specified name, access mode, and custom attributes.

public:
 System::Reflection::Emit::AssemblyBuilder ^ DefineDynamicAssembly(System::Reflection::AssemblyName ^ name, System::Reflection::Emit::AssemblyBuilderAccess access, System::Collections::Generic::IEnumerable<System::Reflection::Emit::CustomAttributeBuilder ^> ^ assemblyAttributes);
public System.Reflection.Emit.AssemblyBuilder DefineDynamicAssembly (System.Reflection.AssemblyName name, System.Reflection.Emit.AssemblyBuilderAccess access, System.Collections.Generic.IEnumerable<System.Reflection.Emit.CustomAttributeBuilder> assemblyAttributes);
member this.DefineDynamicAssembly : System.Reflection.AssemblyName * System.Reflection.Emit.AssemblyBuilderAccess * seq<System.Reflection.Emit.CustomAttributeBuilder> -> System.Reflection.Emit.AssemblyBuilder
Public Function DefineDynamicAssembly (name As AssemblyName, access As AssemblyBuilderAccess, assemblyAttributes As IEnumerable(Of CustomAttributeBuilder)) As AssemblyBuilder

パラメーター

name
AssemblyName AssemblyName AssemblyName AssemblyName

動的アセンブリの一意の ID。The unique identity of the dynamic assembly.

access
AssemblyBuilderAccess AssemblyBuilderAccess AssemblyBuilderAccess AssemblyBuilderAccess

動的アセンブリのアクセス モード。The access mode for the dynamic assembly.

assemblyAttributes
IEnumerable<CustomAttributeBuilder>

アセンブリに適用される属性を列挙した一覧。属性がない場合は nullAn enumerable list of attributes to be applied to the assembly, or null if there are no attributes.

戻り値

名前と機能が指定された動的アセンブリ。A dynamic assembly with the specified name and features.

例外

nameName プロパティが null です。The Name property of name is null.

または-or- nameName プロパティが空白で始まっているか、スラッシュまたは円記号が含まれています。The Name property of name starts with white space, or contains a forward or backward slash.

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

次のコード サンプルを含む動的アセンブリを作成する方法を示しています、SecurityTransparentAttributeします。The following code sample shows how to create a dynamic assembly that has the SecurityTransparentAttribute. 属性の配列の要素として指定する必要がありますCustomAttributeBuilderオブジェクト。The attribute must be specified as an element of an array of CustomAttributeBuilder objects.

最初の手順で作成、CustomAttributeBuilderでは、属性のコンス トラクターを取得します。The first step in creating the CustomAttributeBuilder is to obtain a constructor for the attribute. コンス トラクターは、パラメーターを持たないため、GetConstructorの空の配列メソッドが呼び出されたTypeパラメーターの型を表すオブジェクト。The constructor has no parameters, so the GetConstructor method is called with an empty array of Type objects to represent the types of the parameters. 2 番目の手順では、その結果を渡すConstructorInfoオブジェクトのコンス トラクターをCustomAttributeBuilderクラス、型の空の配列と共にObjectに引数を表します。The second step is to pass the resulting ConstructorInfo object to the constructor for the CustomAttributeBuilder class, together with an empty array of type Object to represent the arguments.

結果のCustomAttributeBuilderに渡されますが、DefineDynamicAssemblyメソッドとして、配列の唯一の要素。The resulting CustomAttributeBuilder is then passed to the DefineDynamicAssembly method as the only element of an array.

コード例では、新しい動的アセンブリにモジュールと型を定義し、アセンブリの属性を表示します。The example code defines a module and a type in the new dynamic assembly, and then displays the assembly's attributes.

using namespace System;
using namespace System::Reflection;
using namespace System::Reflection::Emit;
using namespace System::Security;

void main()
{
    // Create a CustomAttributeBuilder for the assembly attribute. 
    // 
    // SecurityTransparentAttribute has a parameterless constructor, 
    // which is retrieved by passing an array of empty types for the
    // constructor's parameter types. The CustomAttributeBuilder is 
    // then created by passing the ConstructorInfo and an empty array
    // of objects to represent the parameters.
    //
    ConstructorInfo^ transparentCtor = 
        SecurityTransparentAttribute::typeid->GetConstructor(
            Type::EmptyTypes);
    CustomAttributeBuilder^ transparent = gcnew CustomAttributeBuilder(
        transparentCtor,
        gcnew array<Object^> {} );
      
    // Create a dynamic assembly using the attribute. The attribute is
    // passed as an array with one element.
    AssemblyName^ aName = gcnew AssemblyName("EmittedAssembly");
    AssemblyBuilder^ ab = AppDomain::CurrentDomain->DefineDynamicAssembly( 
        aName, 
        AssemblyBuilderAccess::Run,
        gcnew array<CustomAttributeBuilder^> { transparent } );

    ModuleBuilder^ mb = ab->DefineDynamicModule( aName->Name );
    TypeBuilder^ tb = mb->DefineType( 
        "MyDynamicType", 
        TypeAttributes::Public );
    tb->CreateType();

    Console::WriteLine("{0}\nAssembly attributes:", ab);
    for each (Attribute^ attr in ab->GetCustomAttributes(true))
    {
        Console::WriteLine("\t{0}", attr);
    }
};

/* This code example produces the following output:

EmittedAssembly, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
Assembly attributes:
        System.Security.SecurityTransparentAttribute
 */
using System;
using System.Reflection;
using System.Reflection.Emit;
using System.Security;

class Example
{
    static void Main()
    {
        // Create a CustomAttributeBuilder for the assembly attribute. 
        // 
        // SecurityTransparentAttribute has a parameterless constructor, 
        // which is retrieved by passing an array of empty types for the
        // constructor's parameter types. The CustomAttributeBuilder is 
        // then created by passing the ConstructorInfo and an empty array
        // of objects to represent the parameters.
        //
        ConstructorInfo transparentCtor = 
            typeof(SecurityTransparentAttribute).GetConstructor(
                Type.EmptyTypes);
        CustomAttributeBuilder transparent = new CustomAttributeBuilder(
            transparentCtor,
            new Object[] {} );
      
        // Create a dynamic assembly using the attribute. The attribute is
        // passed as an array with one element.
        AssemblyName aName = new AssemblyName("EmittedAssembly");
        AssemblyBuilder ab = AppDomain.CurrentDomain.DefineDynamicAssembly( 
            aName, 
            AssemblyBuilderAccess.Run,
            new CustomAttributeBuilder[] { transparent } );

        ModuleBuilder mb = ab.DefineDynamicModule( aName.Name );
        TypeBuilder tb = mb.DefineType( 
            "MyDynamicType", 
            TypeAttributes.Public );
        tb.CreateType();

        Console.WriteLine("{0}\nAssembly attributes:", ab);
        foreach (Attribute attr in ab.GetCustomAttributes(true))
        {
            Console.WriteLine("\t{0}", attr);
        }
    }
}

/* This code example produces the following output:

EmittedAssembly, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
Assembly attributes:
        System.Security.SecurityTransparentAttribute
 */
Imports System
Imports System.Reflection
Imports System.Reflection.Emit
Imports System.Security

Module Example

    Sub Main()
    
        ' Create a CustomAttributeBuilder for the assembly attribute. 
        ' 
        ' SecurityTransparentAttribute has a parameterless constructor, 
        ' which is retrieved by passing an array of empty types for the
        ' constructor's parameter types. The CustomAttributeBuilder is 
        ' then created by passing the ConstructorInfo and an empty array
        ' of objects to represent the parameters.
        '
        Dim transparentCtor As ConstructorInfo = _
            GetType(SecurityTransparentAttribute).GetConstructor( _
                Type.EmptyTypes)
        Dim transparent As New CustomAttributeBuilder( _
            transparentCtor, _
            New Object() {} )
      
        ' Create a dynamic assembly Imports the attribute. The attribute is
        ' passed as an array with one element.
        Dim aName As New AssemblyName("EmittedAssembly")
        Dim ab As AssemblyBuilder = _
            AppDomain.CurrentDomain.DefineDynamicAssembly( _
                aName, _
                AssemblyBuilderAccess.Run, _
                New CustomAttributeBuilder() { transparent } )

        Dim mb As ModuleBuilder = ab.DefineDynamicModule( aName.Name )
        Dim tb As TypeBuilder = mb.DefineType( _
            "MyDynamicType", _
            TypeAttributes.Public )
        tb.CreateType()

        Console.WriteLine("{0}" & vbLf & "Assembly attributes:", ab)
        For Each attr As Attribute In ab.GetCustomAttributes(True)
            Console.WriteLine(vbTab & "{0}", attr)
        Next
    End Sub
End Module

' This code example produces the following output:
'
'EmittedAssembly, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
'Assembly attributes:
'        System.Security.SecurityTransparentAttribute

注釈

このメソッドのオーバー ロードを使用すると、動的アセンブリの作成時に適用しない限り、正しく動作していない属性を指定できます。Use this method overload to specify attributes that do not work correctly unless they are applied when a dynamic assembly is created. たとえば、セキュリティなどの属性SecurityTransparentAttributeSecurityCriticalAttribute動的アセンブリが作成された後に追加されている場合に正常に機能しません。For example, security attributes such as SecurityTransparentAttribute and SecurityCriticalAttribute do not work correctly if they are added after a dynamic assembly has been created.

このメソッドは、現在のアプリケーション ドメインで、動的アセンブリを定義するときだけ使用する必要があります。This method should be used only to define a dynamic assembly in the current application domain. この制限の詳細については、次を参照してください。、Load(AssemblyName)メソッドのオーバー ロードします。For more information about this restriction, see the Load(AssemblyName) method overload.

このメソッドのオーバー ロードがで導入された、 .NET Framework 3.5.NET Framework 3.5します。This method overload is introduced in the .NET Framework 3.5.NET Framework 3.5.

こちらもご覧ください

DefineDynamicAssembly(AssemblyName, AssemblyBuilderAccess) DefineDynamicAssembly(AssemblyName, AssemblyBuilderAccess) DefineDynamicAssembly(AssemblyName, AssemblyBuilderAccess) DefineDynamicAssembly(AssemblyName, AssemblyBuilderAccess)

名前とアクセス モードを指定して、動的アセンブリを定義します。Defines a dynamic assembly with the specified name and access mode.

public:
 virtual System::Reflection::Emit::AssemblyBuilder ^ DefineDynamicAssembly(System::Reflection::AssemblyName ^ name, System::Reflection::Emit::AssemblyBuilderAccess access);
public System.Reflection.Emit.AssemblyBuilder DefineDynamicAssembly (System.Reflection.AssemblyName name, System.Reflection.Emit.AssemblyBuilderAccess access);
abstract member DefineDynamicAssembly : System.Reflection.AssemblyName * System.Reflection.Emit.AssemblyBuilderAccess -> System.Reflection.Emit.AssemblyBuilder
override this.DefineDynamicAssembly : System.Reflection.AssemblyName * System.Reflection.Emit.AssemblyBuilderAccess -> System.Reflection.Emit.AssemblyBuilder
Public Function DefineDynamicAssembly (name As AssemblyName, access As AssemblyBuilderAccess) As AssemblyBuilder

パラメーター

name
AssemblyName AssemblyName AssemblyName AssemblyName

動的アセンブリの一意の ID。The unique identity of the dynamic assembly.

access
AssemblyBuilderAccess AssemblyBuilderAccess AssemblyBuilderAccess AssemblyBuilderAccess

動的アセンブリのアクセス モード。The access mode for the dynamic assembly.

戻り値

名前とアクセス モードが指定された動的アセンブリ。A dynamic assembly with the specified name and access mode.

実装

例外

nameName プロパティが null です。The Name property of name is null.

または-or- nameName プロパティが空白文字で始まるか、スラッシュまたはバック スラッシュを含んでいます。The Name property of name begins with white space, or contains a forward or backward slash.

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

次のサンプルでは、DefineDynamicAssemblyメソッドとAssemblyResolveイベント。The following sample demonstrates the DefineDynamicAssembly method and AssemblyResolve event.

このコード例を実行するには、完全修飾アセンブリ名を指定する必要があります。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::Reflection;
using namespace System::Reflection::Emit;
ref class Test
{
public:
   static void InstantiateMyDynamicType( AppDomain^ domain )
   {
      try
      {
         
         // You must supply a valid fully qualified assembly name here.
         domain->CreateInstance( "Assembly text name, Version, Culture, PublicKeyToken", "MyDynamicType" );
      }
      catch ( Exception^ e ) 
      {
         Console::WriteLine( e->Message );
      }

   }

   static Assembly^ MyResolveEventHandler( Object^ sender, ResolveEventArgs^ args )
   {
      return DefineDynamicAssembly( dynamic_cast<AppDomain^>(sender) );
   }

   static Assembly^ DefineDynamicAssembly( AppDomain^ domain )
   {
      
      // Build a dynamic assembly using Reflection Emit API.
      AssemblyName^ assemblyName = gcnew AssemblyName;
      assemblyName->Name = "MyDynamicAssembly";
      AssemblyBuilder^ assemblyBuilder = domain->DefineDynamicAssembly( assemblyName, AssemblyBuilderAccess::Run );
      ModuleBuilder^ moduleBuilder = assemblyBuilder->DefineDynamicModule( "MyDynamicModule" );
      TypeBuilder^ typeBuilder = moduleBuilder->DefineType( "MyDynamicType", TypeAttributes::Public );
      ConstructorBuilder^ constructorBuilder = typeBuilder->DefineConstructor( MethodAttributes::Public, CallingConventions::Standard, nullptr );
      ILGenerator^ ilGenerator = constructorBuilder->GetILGenerator();
      ilGenerator->EmitWriteLine( "MyDynamicType instantiated!" );
      ilGenerator->Emit( OpCodes::Ret );
      typeBuilder->CreateType();
      return assemblyBuilder;
   }

};

int main()
{
   AppDomain^ currentDomain = AppDomain::CurrentDomain;
   Test::InstantiateMyDynamicType( currentDomain ); // Failed!
   currentDomain->AssemblyResolve += gcnew ResolveEventHandler( Test::MyResolveEventHandler );
   Test::InstantiateMyDynamicType( currentDomain ); // OK!
}

using System;
using System.Reflection;
using System.Reflection.Emit;

class Test {
   public static void Main() {
      AppDomain currentDomain = AppDomain.CurrentDomain;

      InstantiateMyDynamicType(currentDomain);   // Failed!
      
      currentDomain.AssemblyResolve += new ResolveEventHandler(MyResolveEventHandler);
      
      InstantiateMyDynamicType(currentDomain);   // OK!
   }

   static void InstantiateMyDynamicType(AppDomain domain) {
      try {
         // You must supply a valid fully qualified assembly name here. 
         domain.CreateInstance("Assembly text name, Version, Culture, PublicKeyToken", "MyDynamicType");
      } catch (Exception e) {
         Console.WriteLine(e.Message);
      }
   }   

   static Assembly MyResolveEventHandler(object sender, ResolveEventArgs args) {
      return DefineDynamicAssembly((AppDomain) sender);
   }
   
   static Assembly DefineDynamicAssembly(AppDomain domain) {
      // Build a dynamic assembly using Reflection Emit API.
   
      AssemblyName assemblyName = new AssemblyName();
      assemblyName.Name = "MyDynamicAssembly";

      AssemblyBuilder assemblyBuilder = domain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run);
      ModuleBuilder moduleBuilder = assemblyBuilder.DefineDynamicModule("MyDynamicModule");
      TypeBuilder typeBuilder = moduleBuilder.DefineType("MyDynamicType", TypeAttributes.Public);
      ConstructorBuilder constructorBuilder = typeBuilder.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, null);
      ILGenerator ilGenerator = constructorBuilder.GetILGenerator();
      
      ilGenerator.EmitWriteLine("MyDynamicType instantiated!");
      ilGenerator.Emit(OpCodes.Ret);

      typeBuilder.CreateType();

      return assemblyBuilder;
   }
}

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

Module Test
   
   Sub Main()
      Dim currentDomain As AppDomain = AppDomain.CurrentDomain
      
      InstantiateMyDynamicType(currentDomain)   'Failed!
      
      AddHandler currentDomain.AssemblyResolve, AddressOf MyResolveEventHandler
      
      InstantiateMyDynamicType(currentDomain)   'OK!
   End Sub 'Main
   
   Sub InstantiateMyDynamicType(domain As AppDomain)
      Try
         ' You must supply a valid fully qualified assembly name here.
         domain.CreateInstance("Assembly text name, Version, Culture, PublicKeyToken", "MyDynamicType")
      Catch e As Exception
         Console.WriteLine(e.Message)
      End Try
   End Sub 'InstantiateMyDynamicType
   
   Function MyResolveEventHandler(sender As Object, args As ResolveEventArgs) As System.Reflection.Assembly
      Return DefineDynamicAssembly(DirectCast(sender, AppDomain))
   End Function 'MyResolveEventHandler
   
   Function DefineDynamicAssembly(domain As AppDomain) As System.Reflection.Assembly
      ' Build a dynamic assembly using Reflection Emit API.

      Dim assemblyName As New AssemblyName()
      assemblyName.Name = "MyDynamicAssembly"
      
      Dim assemblyBuilder As AssemblyBuilder = domain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run)
      Dim moduleBuilder As ModuleBuilder = assemblyBuilder.DefineDynamicModule("MyDynamicModule")
      Dim typeBuilder As TypeBuilder = moduleBuilder.DefineType("MyDynamicType", TypeAttributes.Public)
      Dim constructorBuilder As ConstructorBuilder = typeBuilder.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, Nothing)
      Dim ilGenerator As ILGenerator = constructorBuilder.GetILGenerator()
      
      ilGenerator.EmitWriteLine("MyDynamicType instantiated!")
      ilGenerator.Emit(OpCodes.Ret)
      
      typeBuilder.CreateType()
      
      Return assemblyBuilder
   End Function 'DefineDynamicAssembly

End Module 'Test 

注釈

このメソッドは、現在のアプリケーション ドメインで、動的アセンブリを定義するのみ使用する必要があります。This method should only be used to define a dynamic assembly in the current application domain. 詳細については、次を参照してください。、Load(AssemblyName)メソッドのオーバー ロードします。For more information, see the Load(AssemblyName) method overload.

注意

動的アセンブリを生成するコードの開発中はお勧めのオーバー ロードを使用すること、DefineDynamicAssembly証拠、アクセス許可を指定するメソッドを含む動的アセンブリを作成証拠を指定するSecurityPermissionFlag.SkipVerificationrefusedPermissions.During the development of code that emits dynamic assemblies, it is recommended that you use an overload of the DefineDynamicAssembly method that specifies evidence and permissions, supply the evidence you want the dynamic assembly to have, and include SecurityPermissionFlag.SkipVerification in refusedPermissions. 含むSkipVerificationで、refusedPermissionsパラメーターは、MSIL が検証されていることを保証します。Including SkipVerification in the refusedPermissions parameter ensures that the MSIL is verified. この技法の制限はまた、あるSecurityException完全な信頼を要求するコードで使用する場合にスローされます。A limitation of this technique is that it also causes SecurityException to be thrown when used with code that demands full trust.

適用対象