次の方法で共有


AppDomain.DefineDynamicAssembly メソッド

現在のアプリケーション ドメインで動的アセンブリを定義します。

オーバーロードの一覧

名前とアクセス モードを指定して、動的アセンブリを定義します。

[Visual Basic] Overloads Public Overridable Function DefineDynamicAssembly(AssemblyName, AssemblyBuilderAccess) As AssemblyBuilder Implements _AppDomain.DefineDynamicAssembly

[C#] public virtual AssemblyBuilder DefineDynamicAssembly(AssemblyName, AssemblyBuilderAccess);

[C++] public: virtual AssemblyBuilder* DefineDynamicAssembly(AssemblyName*, AssemblyBuilderAccess);

[JScript] public function DefineDynamicAssembly(AssemblyName, AssemblyBuilderAccess) : AssemblyBuilder;

名前、アクセス モード、および証拠を指定して、動的アセンブリを定義します。

[Visual Basic] Overloads Public Overridable Function DefineDynamicAssembly(AssemblyName, AssemblyBuilderAccess, Evidence) As AssemblyBuilder Implements _AppDomain.DefineDynamicAssembly

[C#] public virtual AssemblyBuilder DefineDynamicAssembly(AssemblyName, AssemblyBuilderAccess, Evidence);

[C++] public: virtual AssemblyBuilder* DefineDynamicAssembly(AssemblyName*, AssemblyBuilderAccess, Evidence*);

[JScript] public function DefineDynamicAssembly(AssemblyName, AssemblyBuilderAccess, Evidence) : AssemblyBuilder;

名前、アクセス モード、およびストレージ ディレクトリを指定して、動的アセンブリを定義します。

[Visual Basic] Overloads Public Overridable Function DefineDynamicAssembly(AssemblyName, AssemblyBuilderAccess, String) As AssemblyBuilder Implements _AppDomain.DefineDynamicAssembly

[C#] public virtual AssemblyBuilder DefineDynamicAssembly(AssemblyName, AssemblyBuilderAccess, string);

[C++] public: virtual AssemblyBuilder* DefineDynamicAssembly(AssemblyName*, AssemblyBuilderAccess, String*);

[JScript] public function DefineDynamicAssembly(AssemblyName, AssemblyBuilderAccess, String) : AssemblyBuilder;

名前、アクセス モード、ストレージ ディレクトリ、および証拠を指定して、動的アセンブリを定義します。

[Visual Basic] Overloads Public Overridable Function DefineDynamicAssembly(AssemblyName, AssemblyBuilderAccess, String, Evidence) As AssemblyBuilder Implements _AppDomain.DefineDynamicAssembly

[C#] public virtual AssemblyBuilder DefineDynamicAssembly(AssemblyName, AssemblyBuilderAccess, string, Evidence);

[C++] public: virtual AssemblyBuilder* DefineDynamicAssembly(AssemblyName*, AssemblyBuilderAccess, String*, Evidence*);

[JScript] public function DefineDynamicAssembly(AssemblyName, AssemblyBuilderAccess, String, Evidence) : AssemblyBuilder;

名前、アクセス モード、およびアクセス許可要求を指定して、動的アセンブリを定義します。

[Visual Basic] Overloads Public Overridable Function DefineDynamicAssembly(AssemblyName, AssemblyBuilderAccess, PermissionSet, PermissionSet, PermissionSet) As AssemblyBuilder Implements _AppDomain.DefineDynamicAssembly

[C#] public virtual AssemblyBuilder DefineDynamicAssembly(AssemblyName, AssemblyBuilderAccess, PermissionSet, PermissionSet, PermissionSet);

[C++] public: virtual AssemblyBuilder* DefineDynamicAssembly(AssemblyName*, AssemblyBuilderAccess, PermissionSet*, PermissionSet*, PermissionSet*);

[JScript] public function DefineDynamicAssembly(AssemblyName, AssemblyBuilderAccess, PermissionSet, PermissionSet, PermissionSet) : AssemblyBuilder;

名前、アクセス モード、証拠、およびアクセス許可要求を指定して、動的アセンブリを定義します。

[Visual Basic] Overloads Public Overridable Function DefineDynamicAssembly(AssemblyName, AssemblyBuilderAccess, Evidence, PermissionSet, PermissionSet, PermissionSet) As AssemblyBuilder Implements _AppDomain.DefineDynamicAssembly

[C#] public virtual AssemblyBuilder DefineDynamicAssembly(AssemblyName, AssemblyBuilderAccess, Evidence, PermissionSet, PermissionSet, PermissionSet);

[C++] public: virtual AssemblyBuilder* DefineDynamicAssembly(AssemblyName*, AssemblyBuilderAccess, Evidence*, PermissionSet*, PermissionSet*, PermissionSet*);

[JScript] public function DefineDynamicAssembly(AssemblyName, AssemblyBuilderAccess, Evidence, PermissionSet, PermissionSet, PermissionSet) : AssemblyBuilder;

名前、アクセス モード、ストレージ ディレクトリ、およびアクセス許可要求を指定して、動的アセンブリを定義します。

[Visual Basic] Overloads Public Overridable Function DefineDynamicAssembly(AssemblyName, AssemblyBuilderAccess, String, PermissionSet, PermissionSet, PermissionSet) As AssemblyBuilder Implements _AppDomain.DefineDynamicAssembly

[C#] public virtual AssemblyBuilder DefineDynamicAssembly(AssemblyName, AssemblyBuilderAccess, string, PermissionSet, PermissionSet, PermissionSet);

[C++] public: virtual AssemblyBuilder* DefineDynamicAssembly(AssemblyName*, AssemblyBuilderAccess, String*, PermissionSet*, PermissionSet*, PermissionSet*);

[JScript] public function DefineDynamicAssembly(AssemblyName, AssemblyBuilderAccess, String, PermissionSet, PermissionSet, PermissionSet) : AssemblyBuilder;

名前、アクセス モード、ストレージ ディレクトリ、証拠、およびアクセス許可要求を指定して、動的アセンブリを定義します。

[Visual Basic] Overloads Public Overridable Function DefineDynamicAssembly(AssemblyName, AssemblyBuilderAccess, String, Evidence, PermissionSet, PermissionSet, PermissionSet) As AssemblyBuilder Implements _AppDomain.DefineDynamicAssembly

[C#] public virtual AssemblyBuilder DefineDynamicAssembly(AssemblyName, AssemblyBuilderAccess, string, Evidence, PermissionSet, PermissionSet, PermissionSet);

[C++] public: virtual AssemblyBuilder* DefineDynamicAssembly(AssemblyName*, AssemblyBuilderAccess, String*, Evidence*, PermissionSet*, PermissionSet*, PermissionSet*);

[JScript] public function DefineDynamicAssembly(AssemblyName, AssemblyBuilderAccess, String, Evidence, PermissionSet, PermissionSet, PermissionSet) : AssemblyBuilder;

名前、アクセス モード、ストレージ ディレクトリ、証拠、アクセス許可要求、および同期オプションを指定して、動的アセンブリを定義します。

[Visual Basic] Overloads Public Overridable Function DefineDynamicAssembly(AssemblyName, AssemblyBuilderAccess, String, Evidence, PermissionSet, PermissionSet, PermissionSet, Boolean) As AssemblyBuilder Implements _AppDomain.DefineDynamicAssembly

[C#] public virtual AssemblyBuilder DefineDynamicAssembly(AssemblyName, AssemblyBuilderAccess, string, Evidence, PermissionSet, PermissionSet, PermissionSet, bool);

[C++] public: virtual AssemblyBuilder* DefineDynamicAssembly(AssemblyName*, AssemblyBuilderAccess, String*, Evidence*, PermissionSet*, PermissionSet*, PermissionSet*, bool);

[JScript] public function DefineDynamicAssembly(AssemblyName, AssemblyBuilderAccess, String, Evidence, PermissionSet, PermissionSet, PermissionSet, Boolean) : AssemblyBuilder;

使用例

[Visual Basic, C#, C++] DefineDynamicAssembly メソッドおよび AssemblyResolve イベントを次のサンプルで示します。

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

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

 

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 

[C#] 
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;
   }
}

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

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

__gc class Test {
public:
   static void InstantiateMyDynamicType(AppDomain* domain) {
      try {
         // You must supply a valid fully qualified assembly name here.
         domain->CreateInstance(S"Assembly text name, Version, Culture, PublicKeyToken", S"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 = new AssemblyName();
      assemblyName->Name = S"MyDynamicAssembly";

      AssemblyBuilder*  assemblyBuilder = domain->DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess::Run);
      ModuleBuilder*  moduleBuilder = assemblyBuilder->DefineDynamicModule(S"MyDynamicModule");
      TypeBuilder*  typeBuilder = moduleBuilder->DefineType(S"MyDynamicType", TypeAttributes::Public);
      ConstructorBuilder*  constructorBuilder = typeBuilder->DefineConstructor(MethodAttributes::Public, CallingConventions::Standard, 0);
      ILGenerator*  ilGenerator = constructorBuilder->GetILGenerator();

      ilGenerator->EmitWriteLine(S"MyDynamicType instantiated!");
      ilGenerator->Emit(OpCodes::Ret);

      typeBuilder->CreateType();

      return assemblyBuilder;
   }
};

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

   Test::InstantiateMyDynamicType(currentDomain);   // Failed!

   currentDomain->AssemblyResolve += new ResolveEventHandler(0, Test::MyResolveEventHandler);

   Test::InstantiateMyDynamicType(currentDomain);   // OK!
}

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

参照

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