ModuleBuilder.DefineType Method

Definition

Constructs a TypeBuilder. To define a value type, define a type that derives from ValueType.

Overloads

DefineType(String, TypeAttributes, Type, Type[])

Constructs a TypeBuilder given the type name, attributes, the type that the defined type extends, and the interfaces that the defined type implements.

DefineType(String, TypeAttributes, Type, PackingSize, Int32)

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

DefineType(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.

DefineType(String, TypeAttributes)

Constructs a TypeBuilder given the type name and the type attributes.

DefineType(String, TypeAttributes, Type)

Constructs a TypeBuilder given type name, its attributes, and the type that the defined type extends.

DefineType(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.

DefineType(String)

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

DefineType(String, TypeAttributes, Type, Type[])

Source:
ModuleBuilder.cs
Source:
ModuleBuilder.cs
Source:
ModuleBuilder.cs

Constructs a TypeBuilder given the type name, attributes, the type that the defined type extends, and the interfaces that the defined type implements.

public System.Reflection.Emit.TypeBuilder DefineType (string name, System.Reflection.TypeAttributes attr, Type? parent, Type[]? interfaces);
public System.Reflection.Emit.TypeBuilder DefineType (string name, System.Reflection.TypeAttributes attr, Type parent, Type[] interfaces);
[System.Runtime.InteropServices.ComVisible(true)]
public System.Reflection.Emit.TypeBuilder DefineType (string name, System.Reflection.TypeAttributes attr, Type parent, Type[] interfaces);

Parameters

name
String

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

attr
TypeAttributes

The attributes to be associated with the type.

parent
Type

The type that the defined type extends.

interfaces
Type[]

The list of interfaces that the type implements.

Returns

A TypeBuilder created with all of the requested attributes.

Attributes

Exceptions

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.

name is null.

Examples

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

Remarks

Type names must be unique within an assembly. You cannot have two types with the same name in two different modules of an assembly.

Anteckning

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.

Applies to

.NET 9 och andra versioner
Produkt Versioner
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0 (package-provided), 2.1

DefineType(String, TypeAttributes, Type, PackingSize, Int32)

Source:
ModuleBuilder.cs
Source:
ModuleBuilder.cs
Source:
ModuleBuilder.cs

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

public System.Reflection.Emit.TypeBuilder DefineType (string name, System.Reflection.TypeAttributes attr, Type? parent, System.Reflection.Emit.PackingSize packingSize, int typesize);
public System.Reflection.Emit.TypeBuilder DefineType (string name, System.Reflection.TypeAttributes attr, Type parent, System.Reflection.Emit.PackingSize packingSize, int typesize);

Parameters

name
String

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

attr
TypeAttributes

The attributes of the defined type.

parent
Type

The type that the defined type extends.

packingSize
PackingSize

The packing size of the type.

typesize
Int32

The total size of the type.

Returns

A TypeBuilder created with all of the requested attributes.

Exceptions

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.

name is null.

Examples

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

Remarks

Type names must be unique within an assembly. You cannot have two types with the same name in two different modules of an assembly.

Anteckning

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.

Applies to

.NET 9 och andra versioner
Produkt Versioner
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0 (package-provided), 2.1

DefineType(String, TypeAttributes, Type, PackingSize)

Source:
ModuleBuilder.cs
Source:
ModuleBuilder.cs
Source:
ModuleBuilder.cs

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

public System.Reflection.Emit.TypeBuilder DefineType (string name, System.Reflection.TypeAttributes attr, Type? parent, System.Reflection.Emit.PackingSize packsize);
public System.Reflection.Emit.TypeBuilder DefineType (string name, System.Reflection.TypeAttributes attr, Type parent, System.Reflection.Emit.PackingSize packsize);

Parameters

name
String

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

attr
TypeAttributes

The attributes of the defined type.

parent
Type

The type that the defined type extends.

packsize
PackingSize

The packing size of the type.

Returns

A TypeBuilder object.

Exceptions

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.

name is null.

Examples

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

Remarks

Type names must be unique within an assembly. You cannot have two types with the same name in two different modules of an assembly.

Anteckning

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.

Applies to

.NET 9 och andra versioner
Produkt Versioner
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0 (package-provided), 2.1

DefineType(String, TypeAttributes)

Source:
ModuleBuilder.cs
Source:
ModuleBuilder.cs
Source:
ModuleBuilder.cs

Constructs a TypeBuilder given the type name and the type attributes.

public System.Reflection.Emit.TypeBuilder DefineType (string name, System.Reflection.TypeAttributes attr);

Parameters

name
String

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

attr
TypeAttributes

The attributes of the defined type.

Returns

A TypeBuilder created with all of the requested attributes.

Exceptions

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.

name is null.

Examples

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

Remarks

Type names must be unique within an assembly. You cannot have two types with the same name in two different modules of an assembly.

Anteckning

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.

Applies to

.NET 9 och andra versioner
Produkt Versioner
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0 (package-provided), 2.1

DefineType(String, TypeAttributes, Type)

Source:
ModuleBuilder.cs
Source:
ModuleBuilder.cs
Source:
ModuleBuilder.cs

Constructs a TypeBuilder given type name, its attributes, and the type that the defined type extends.

public System.Reflection.Emit.TypeBuilder DefineType (string name, System.Reflection.TypeAttributes attr, Type? parent);
public System.Reflection.Emit.TypeBuilder DefineType (string name, System.Reflection.TypeAttributes attr, Type parent);

Parameters

name
String

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

attr
TypeAttributes

The attribute to be associated with the type.

parent
Type

The type that the defined type extends.

Returns

A TypeBuilder created with all of the requested attributes.

Exceptions

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.

name is null.

Examples

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

Remarks

Type names must be unique within an assembly. You cannot have two types with the same name in two different modules of an assembly.

Anteckning

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.

Applies to

.NET 9 och andra versioner
Produkt Versioner
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0 (package-provided), 2.1

DefineType(String, TypeAttributes, Type, Int32)

Source:
ModuleBuilder.cs
Source:
ModuleBuilder.cs
Source:
ModuleBuilder.cs

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

public System.Reflection.Emit.TypeBuilder DefineType (string name, System.Reflection.TypeAttributes attr, Type? parent, int typesize);
public System.Reflection.Emit.TypeBuilder DefineType (string name, System.Reflection.TypeAttributes attr, Type parent, int typesize);

Parameters

name
String

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

attr
TypeAttributes

The attributes of the defined type.

parent
Type

The type that the defined type extends.

typesize
Int32

The total size of the type.

Returns

A TypeBuilder object.

Exceptions

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.

name is null.

Examples

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

Remarks

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.

Anteckning

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.

Applies to

.NET 9 och andra versioner
Produkt Versioner
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0 (package-provided), 2.1

DefineType(String)

Source:
ModuleBuilder.cs
Source:
ModuleBuilder.cs
Source:
ModuleBuilder.cs

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

public System.Reflection.Emit.TypeBuilder DefineType (string name);

Parameters

name
String

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

Returns

A private type with the specified name.

Exceptions

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.

name is null.

Examples

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

Remarks

Type names must be unique within an assembly. You cannot have two types with the same name in two different modules of an assembly.

Anteckning

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.

Applies to

.NET 9 och andra versioner
Produkt Versioner
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0 (package-provided), 2.1