Export (0) Print
Expand All

ModuleBuilder.DefineType Method (String, TypeAttributes, Type, PackingSize)

Constructs a TypeBuilder given the type name, the attributes, the type that the defined type extends, and the packing size of the type.

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

public TypeBuilder DefineType(
	string name,
	TypeAttributes attr,
	Type parent,
	PackingSize packsize
)

Parameters

name
Type: System.String

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

attr
Type: System.Reflection.TypeAttributes

The attributes of the defined type.

parent
Type: System.Type

The Type that the defined type extends.

packsize
Type: System.Reflection.Emit.PackingSize

The packing size of the type.

Return Value

Type: System.Reflection.Emit.TypeBuilder
Returns a TypeBuilder object.

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 version 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 version 3.5. For more information, see .NET Framework 3.5 Architecture.

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" );

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

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, 3.0, 2.0, 1.1, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft