This documentation is archived and is not being maintained.

AppDomain.DefineDynamicAssembly Method (AssemblyName, AssemblyBuilderAccess, String, PermissionSet, PermissionSet, PermissionSet)

.NET Framework 1.1

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

[Visual Basic]
Overloads Public Overridable Function DefineDynamicAssembly( _
   ByVal name As AssemblyName, _
   ByVal access As AssemblyBuilderAccess, _
   ByVal dir As String, _
   ByVal requiredPermissions As PermissionSet, _
   ByVal optionalPermissions As PermissionSet, _
   ByVal refusedPermissions As PermissionSet _
) As AssemblyBuilder Implements _AppDomain.DefineDynamicAssembly
public virtual AssemblyBuilder DefineDynamicAssembly(
 AssemblyName name,
 AssemblyBuilderAccess access,
 string dir,
 PermissionSet requiredPermissions,
 PermissionSet optionalPermissions,
 PermissionSet refusedPermissions
public: virtual AssemblyBuilder* DefineDynamicAssembly(
 AssemblyName* name,
 AssemblyBuilderAccess access,
 String* dir,
 PermissionSet* requiredPermissions,
 PermissionSet* optionalPermissions,
 PermissionSet* refusedPermissions
public function DefineDynamicAssembly(
   name : AssemblyName,
 access : AssemblyBuilderAccess,
 dir : String,
 requiredPermissions : PermissionSet,
 optionalPermissions : PermissionSet,
 refusedPermissions : PermissionSet
) : AssemblyBuilder;


The unique identity of the dynamic assembly.
The mode in which the dynamic assembly will be accessed.
The name of the directory where the assembly will be saved. If dir is a null reference (Nothing in Visual Basic), the directory defaults to the current directory.
The required permissions request.
The optional permissions request.
The refused permissions request.

Return Value

Represents the dynamic assembly created.




Exception Type Condition
ArgumentNullException name is a null reference (Nothing in Visual Basic).
ArgumentException The Name property of name is a null reference (Nothing in Visual Basic).


The Name property of name begins with white space, or contains a forward or backward slash.

AppDomainUnloadedException Operations are attempted on an unloaded application domain.
SecurityException The caller does not have the required permission.


This method should only be used to define a dynamic assembly in the current application domain. For more information, see the Remarks section of the AppDomain.Load Method (AssemblyName) topic.

The name parameter indirectly specifies a Version (as an OperatingSystem constructor parameter). However, this method only persists the major and minor version numbers, not the build and revision numbers. For example, if the LoadFrom method is used to indirectly recover the Version for this assembly, only that object's major and minor version numbers will be as originally specified.


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

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)
         ' You must supply a valid fully qualified assembly name here.
         domain.CreateInstance("Assembly text name, Version, Culture, PublicKeyToken", "MyDynamicType")
      Catch e As Exception
      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!")
      Return assemblyBuilder
   End Function 'DefineDynamicAssembly

End Module 'Test 

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) {

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


      return assemblyBuilder;

#using <mscorlib.dll>

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

__gc class Test {
   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) {

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


      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.


Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

.NET Framework Security: 

See Also

AppDomain Class | AppDomain Members | System Namespace | AppDomain.DefineDynamicAssembly Overload List