Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

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

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

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

public AssemblyBuilder DefineDynamicAssembly (
	AssemblyName name,
	AssemblyBuilderAccess access,
	PermissionSet requiredPermissions,
	PermissionSet optionalPermissions,
	PermissionSet refusedPermissions
)
public final AssemblyBuilder DefineDynamicAssembly (
	AssemblyName name, 
	AssemblyBuilderAccess access, 
	PermissionSet requiredPermissions, 
	PermissionSet optionalPermissions, 
	PermissionSet refusedPermissions
)
public final function DefineDynamicAssembly (
	name : AssemblyName, 
	access : AssemblyBuilderAccess, 
	requiredPermissions : PermissionSet, 
	optionalPermissions : PermissionSet, 
	refusedPermissions : PermissionSet
) : AssemblyBuilder
Not applicable.

Parameters

name

The unique identity of the dynamic assembly.

access

The mode in which the dynamic assembly will be accessed.

requiredPermissions

The required permissions request.

optionalPermissions

The optional permissions request.

refusedPermissions

The refused permissions request.

Return Value

Represents the dynamic assembly created.

Exception typeCondition

ArgumentNullException

name is a null reference (Nothing in Visual Basic).

ArgumentException

The Name property of name is a null reference (Nothing in Visual Basic).

-or-

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

AppDomainUnloadedException

The operation is attempted on an unloaded application domain.

The permission requests specified for requiredPermissions, optionalPermissions, and refusedPermissions are not used unless the dynamic assembly has been saved and reloaded into memory. To specify permission requests for a transient assembly that is never saved to disk, use an overload of the DefineDynamicAssembly method that specifies evidence as well as requested permissions, and supply an Evidence object.

NoteNote:

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.

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 .

NoteNote:

In the .NET Framework versions 1.0, 1.1, and 2.0, the declarative security attributes applied to a dynamic assembly by using the requiredPermissions, optionalPermissions, and refusedPermissions parameters are stored in the old XML metadata format. See Emitting Declarative Security Attributes.

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.

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

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

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

Show:
© 2014 Microsoft