Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

AppDomain.DefineDynamicAssembly Method (AssemblyName, AssemblyBuilderAccess, IEnumerable<CustomAttributeBuilder>)

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

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)
public AssemblyBuilder DefineDynamicAssembly(
	AssemblyName name,
	AssemblyBuilderAccess access,
	IEnumerable<CustomAttributeBuilder> assemblyAttributes
)

Parameters

name
Type: System.Reflection.AssemblyName

The unique identity of the dynamic assembly.

access
Type: System.Reflection.Emit.AssemblyBuilderAccess

The access mode for the dynamic assembly.

assemblyAttributes
Type: System.Collections.Generic.IEnumerable<CustomAttributeBuilder>

An enumerable list of attributes to be applied to the assembly, or null if there are no attributes.

Return Value

Type: System.Reflection.Emit.AssemblyBuilder
An AssemblyBuilder object that represents the new dynamic assembly.
ExceptionCondition
ArgumentNullException

name is null.

ArgumentException

The Name property of name is null.

-or-

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

AppDomainUnloadedException

The operation is attempted on an unloaded application domain.

Use this method overload to specify attributes that do not work correctly unless they are applied when a dynamic assembly is created. For example, security attributes such as SecurityTransparentAttribute and SecurityCriticalAttribute do not work correctly if they are added after a dynamic assembly has been created.

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

NoteNote:

When you develop code that emits dynamic assemblies, we recommend 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 the SecurityPermissionFlag.SkipVerification flag in the refusedPermissions parameter. The inclusion of this flag ensures that the Microsoft intermediate language (MSIL) will be verified. This technique will detect the unintentional generation of unverifiable code, which otherwise is very difficult to detect. A limitation of this technique is that it also causes SecurityException to be thrown when it is used with code that demands full trust.

NoteNote:

This method overload is introduced in the .NET Framework version 3.5. For more information, see .NET Framework 3.5 Architecture.

The following code example shows how to create a dynamic assembly that has the SecurityTransparentAttribute. The attribute must be specified as an element of an array of CustomAttributeBuilder objects.

The first step in creating the CustomAttributeBuilder is to obtain a constructor for the attribute. The constructor has no parameters, so the GetConstructor method is called with an empty array of Type objects to represent the types of the parameters. The second step is to pass the resulting ConstructorInfo object to the constructor for the CustomAttributeBuilder class, together with an empty array of type Object to represent the arguments.

The resulting CustomAttributeBuilder is then passed to the DefineDynamicAssembly method as the only element of an array.

The example code defines a module and a type in the new dynamic assembly, and then displays the assembly's attributes.

using System;
using System.Reflection;
using System.Reflection.Emit;
using System.Security;

class Example
{
    static void Main()
    {
        // Create a CustomAttributeBuilder for the assembly attribute.  
        //  
        // SecurityTransparentAttribute has a parameterless constructor,  
        // which is retrieved by passing an array of empty types for the 
        // constructor's parameter types. The CustomAttributeBuilder is  
        // then created by passing the ConstructorInfo and an empty array 
        // of objects to represent the parameters. 
        //
        ConstructorInfo transparentCtor = 
            typeof(SecurityTransparentAttribute).GetConstructor(
                Type.EmptyTypes);
        CustomAttributeBuilder transparent = new CustomAttributeBuilder(
            transparentCtor,
            new Object[] {} );

        // Create a dynamic assembly using the attribute. The attribute is 
        // passed as an array with one element.
        AssemblyName aName = new AssemblyName("EmittedAssembly");
        AssemblyBuilder ab = AppDomain.CurrentDomain.DefineDynamicAssembly( 
            aName, 
            AssemblyBuilderAccess.Run,
            new CustomAttributeBuilder[] { transparent } );

        ModuleBuilder mb = ab.DefineDynamicModule( aName.Name );
        TypeBuilder tb = mb.DefineType( 
            "MyDynamicType", 
            TypeAttributes.Public );
        tb.CreateType();

        Console.WriteLine("{0}\nAssembly attributes:", ab);
        foreach (Attribute attr in ab.GetCustomAttributes(true))
        {
            Console.WriteLine("\t{0}", attr);
        }
    }
}

/* This code example produces the following output:

EmittedAssembly, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
Assembly attributes:
        System.Security.SecurityTransparentAttribute
 */

Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003

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 SP1, 3.0 SP1, 2.0 SP1
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.