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)

public TypeBuilder DefineType(
	string name,
	TypeAttributes attr


Type: System.String
The full name of the type, including the namespace. name cannot contain embedded nulls.
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.


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


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


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 = 
        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=, Culture=neutral, PublicKeyToken=null


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