This topic has not yet been rated - Rate this topic

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
)
public TypeBuilder DefineType (
	String name, 
	TypeAttributes attr, 
	Type parent, 
	PackingSize packsize
)
public function DefineType (
	name : String, 
	attr : TypeAttributes, 
	parent : Type, 
	packsize : PackingSize
) : TypeBuilder
Not applicable.

Parameters

name

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

attr

The attributes of the defined type.

parent

The Type that the defined type extends.

packsize

The packing size of the type.

Return Value

Returns a TypeBuilder object.
Exception typeCondition

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 , 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 . For more information, see The .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" );

AssemblyName asmName = new AssemblyName();

asmName.set_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 98, Windows Server 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0
Did you find this helpful?
(1500 characters remaining)

Community Additions

ADD
© 2013 Microsoft. All rights reserved.