Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
DefineType Method (String)

ModuleBuilder.DefineType Method (String)

Constructs a TypeBuilder for a private type with the specified name in this module.

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

public TypeBuilder DefineType(
	string name
)

Parameters

name
Type: System.String

The full path of the type, including the namespace. name cannot contain embedded nulls.

Return Value

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

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. You cannot have two types with the same name in two different modules of an assembly.

NoteNote

Starting with the .NET Framework 2.0 Service Pack 1, this member no longer requires ReflectionPermission with the ReflectionPermissionFlag.ReflectionEmit flag. (See Security Issues in Reflection Emit.) To use this functionality, your application should target the .NET Framework 3.5 or later.

The following example creates a TypeBuilder in the current dynamic module using CreateType, builds and completes the type, and saves the assembly.

AssemblyName asmname = new AssemblyName();
asmname.Name = "assemfilename.exe";        
AssemblyBuilder asmbuild = System.Threading.Thread.GetDomain().
            DefineDynamicAssembly(asmname, AssemblyBuilderAccess.RunAndSave);
ModuleBuilder modbuild = asmbuild.DefineDynamicModule( "modulename",
   "assemfilename.exe" );
TypeBuilder typebuild1 = modbuild.DefineType( "typename" );
typebuild1.CreateType();
asmbuild.Save( "assemfilename.exe" );

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1

.NET Framework Client Profile

Supported in: 4, 3.5 SP1
Show:
© 2015 Microsoft