AppDomain.DefineDynamicAssembly Method (AssemblyName, AssemblyBuilderAccess)


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

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

Public Function DefineDynamicAssembly (
	name As AssemblyName,
	access As AssemblyBuilderAccess
) As AssemblyBuilder


Type: System.Reflection.AssemblyName

The unique identity of the dynamic assembly.

Type: System.Reflection.Emit.AssemblyBuilderAccess

The access mode for the dynamic assembly.

Return Value

Type: System.Reflection.Emit.AssemblyBuilder

A dynamic assembly with the specified name and access mode.

Exception Condition

name is null.


The Name property of name is null.


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 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.

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 

.NET Framework
Available since 1.1
Available since 2.0
Return to top