This documentation is archived and is not being maintained.

AppDomain::DefineDynamicAssembly Method (AssemblyName, AssemblyBuilderAccess, String)

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

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

virtual AssemblyBuilder^ DefineDynamicAssembly(
	AssemblyName^ name, 
	AssemblyBuilderAccess access, 
	String^ dir
) sealed


Type: System.Reflection::AssemblyName

The unique identity of the dynamic assembly.

Type: System.Reflection.Emit::AssemblyBuilderAccess

The mode in which the dynamic assembly will be accessed.

Type: System::String

The name of the directory where the assembly will be saved. If dir is nullptr, the directory defaults to the current directory.

Return Value

Type: System.Reflection.Emit::AssemblyBuilder
Represents the dynamic assembly created.


_AppDomain::DefineDynamicAssembly(AssemblyName, AssemblyBuilderAccess, String)


name is nullptr.


The Name property of name is nullptr.


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.

This method should only be used to define a dynamic assembly in the current application domain. For more information, see the Load(AssemblyName) method overload.


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. Including SkipVerification in the refusedPermissions parameter ensures that the MSIL is verified. A limitation of this technique is that it also causes SecurityException to be thrown when used with code that demands full trust.

The following example 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
   static void InstantiateMyDynamicType( AppDomain^ domain )

         // 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 );
      return assemblyBuilder;


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

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0