ModuleBuilder.DefineType Method (String, TypeAttributes)

Defines a type with the specified type name and attributes in this module.

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

[SecuritySafeCriticalAttribute]
public TypeBuilder DefineType(
	string name,
	TypeAttributes attr
)

Parameters

name
Type: System.String
The full name of the type, including the namespace. name cannot contain embedded nulls.
attr
Type: System.Reflection.TypeAttributes
The attributes of the defined type.

Return Value

Type: System.Reflection.Emit.TypeBuilder
A type with the specified name and attributes.

ExceptionCondition
ArgumentException

A type with the given name exists in the parent assembly of this module.

-or-

Nested type attributes are set on a type that is not nested.

ArgumentNullException

name is null.

Type names must be unique within an assembly.

The following example creates a TypeBuilder in the current dynamic module using CreateType, builds and completes the type, and creates an instance of the type.


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

public class Example
{
    public static void Demo(System.Windows.Controls.TextBlock outputBlock)
    {
        // Create a dynamic assembly. In Silverlight, dynamic assemblies have only
        // one module, and are always created with AssemblyBuilderAccess.Run.
        //
        AssemblyName asmname = new AssemblyName("temp");
        AssemblyBuilder asmbuild = 
            AppDomain.CurrentDomain.DefineDynamicAssembly(asmname, 
                                                          AssemblyBuilderAccess.Run);
        ModuleBuilder modbuild = asmbuild.DefineDynamicModule("temp");

        // Define a type. If TypeAttributes.Public is not specified, the default
        // access is private, and instances of the type cannot be created.
        //
        TypeBuilder typebuild1 = modbuild.DefineType("TestType", TypeAttributes.Public);

        // Create the type, and create an instance of the type.
        //
        Type t = typebuild1.CreateType();
        Object obj = Activator.CreateInstance(t);

        outputBlock.Text += t.AssemblyQualifiedName + "\n";
        outputBlock.Text += obj.ToString();
   }
}

/* This example produces the following output:

TestType, temp, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
TestType
 */


Silverlight

Supported in: 5, 4, 3

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

Community Additions

ADD
Show: