DefineType Method (String, TypeAttributes, Type, Int32)

ModuleBuilder::DefineType Method (String^, TypeAttributes, Type^, Int32)

 

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

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

public:
TypeBuilder^ DefineType(
	String^ name,
	TypeAttributes attr,
	Type^ parent,
	int typesize
)

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.

typesize
Type: System::Int32

The total size of the type.

Return Value

Type: System.Reflection.Emit::TypeBuilder^

A TypeBuilder object.

Exception Condition
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. It is forbidden to have two types with the same name in two different modules of an assembly.

System_CAPS_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 = gcnew 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
Available since 1.1
Silverlight
Available since 2.0
Return to top
Show:
© 2016 Microsoft