Export (0) Print
Expand All

AppDomain.DefineDynamicAssembly Method

Defines a dynamic assembly in the current application domain.

Overload List

Defines a dynamic assembly with the specified name and access mode.

[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;

Defines a dynamic assembly using the specified name, access mode, and evidence.

[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;

Defines a dynamic assembly using the specified name, access mode, and storage directory.

[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;

Defines a dynamic assembly using the specified name, access mode, storage directory, and evidence.

[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;

Defines a dynamic assembly using the specified name, access mode, and permission requests.

[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;

Defines a dynamic assembly using the specified name, access mode, evidence, and permission requests.

[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;

Defines a dynamic assembly using the specified name, access mode, storage directory, and permission requests.

[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;

Defines a dynamic assembly using the specified name, access mode, storage directory, evidence, and permission requests.

[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;

Defines a dynamic assembly using the specified name, access mode, storage directory, evidence, permission requests, and synchronization option.

[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;

Example

[Visual Basic, C#, C++] The following sample demonstrates the DefineDynamicAssembly method and AssemblyResolve event.

[Visual Basic, C#, C++] 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.

[Visual Basic, C#, C++] Note   This example shows how to use one of the overloaded versions of DefineDynamicAssembly. For other examples that might be available, see the individual overload topics.
[Visual Basic] 

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] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

See Also

AppDomain Class | AppDomain Members | System Namespace

Show:
© 2014 Microsoft