TypeBuilder.DefineConstructor Method

Definition

Adds a new constructor to the dynamic type.

Overloads

DefineConstructor(MethodAttributes, CallingConventions, Type[])

Adds a new constructor to the type, with the given attributes and signature.

DefineConstructor(MethodAttributes, CallingConventions, Type[], Type[][], Type[][])

Adds a new constructor to the type, with the given attributes, signature, and custom modifiers.

DefineConstructor(MethodAttributes, CallingConventions, Type[])

Source:
TypeBuilder.cs
Source:
TypeBuilder.cs
Source:
TypeBuilder.cs

Adds a new constructor to the type, with the given attributes and signature.

C#
public System.Reflection.Emit.ConstructorBuilder DefineConstructor(System.Reflection.MethodAttributes attributes, System.Reflection.CallingConventions callingConvention, Type[]? parameterTypes);
C#
public System.Reflection.Emit.ConstructorBuilder DefineConstructor(System.Reflection.MethodAttributes attributes, System.Reflection.CallingConventions callingConvention, Type[] parameterTypes);
C#
[System.Runtime.InteropServices.ComVisible(true)]
public System.Reflection.Emit.ConstructorBuilder DefineConstructor(System.Reflection.MethodAttributes attributes, System.Reflection.CallingConventions callingConvention, Type[] parameterTypes);

Parameters

attributes
MethodAttributes

The attributes of the constructor.

callingConvention
CallingConventions

The calling convention of the constructor.

parameterTypes
Type[]

The parameter types of the constructor.

Returns

The defined constructor.

Attributes

Exceptions

The type was previously created using CreateType().

Examples

The following code sample demonstrates the use of DefineConstructor to set a constructor's particular signature and attributes on a dynamic type and return a corresponding ConstructorBuilder for MSIL population.

C#
// Define the constructor.
Type[] constructorArgs = { typeof(String) };
ConstructorBuilder myConstructorBuilder =
   helloWorldTypeBuilder.DefineConstructor(MethodAttributes.Public,
                      CallingConventions.Standard, constructorArgs);
// Generate IL for the method. The constructor stores its argument in the private field.
ILGenerator myConstructorIL = myConstructorBuilder.GetILGenerator();
myConstructorIL.Emit(OpCodes.Ldarg_0);
myConstructorIL.Emit(OpCodes.Ldarg_1);
myConstructorIL.Emit(OpCodes.Stfld, myGreetingField);
myConstructorIL.Emit(OpCodes.Ret);

Remarks

If you do not define a constructor for your dynamic type, a parameterless constructor is provided automatically, and it calls the parameterless constructor of the base class.

If you define a constructor for your dynamic type, a parameterless constructor is not provided. You have the following options for providing a parameterless constructor in addition to the constructor you defined:

  • If you want a parameterless constructor that simply calls the parameterless constructor of the base class, you can use the DefineDefaultConstructor method to create one (and optionally restrict access to it). Do not provide an implementation for this parameterless constructor. If you do, an exception is thrown when you try to use the constructor. No exception is thrown when the CreateType method is called.

  • If you want a parameterless constructor that does something more than simply calling the parameterless constructor of the base class, or that calls another constructor of the base class, or that does something else entirely, you must use the TypeBuilder.DefineConstructor method to create one, and provide your own implementation.

Applies to

.NET 9 and other versions
Product Versions
.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

DefineConstructor(MethodAttributes, CallingConventions, Type[], Type[][], Type[][])

Source:
TypeBuilder.cs
Source:
TypeBuilder.cs
Source:
TypeBuilder.cs

Adds a new constructor to the type, with the given attributes, signature, and custom modifiers.

C#
public System.Reflection.Emit.ConstructorBuilder DefineConstructor(System.Reflection.MethodAttributes attributes, System.Reflection.CallingConventions callingConvention, Type[]? parameterTypes, Type[][]? requiredCustomModifiers, Type[][]? optionalCustomModifiers);
C#
public System.Reflection.Emit.ConstructorBuilder DefineConstructor(System.Reflection.MethodAttributes attributes, System.Reflection.CallingConventions callingConvention, Type[] parameterTypes, Type[][] requiredCustomModifiers, Type[][] optionalCustomModifiers);
C#
[System.Runtime.InteropServices.ComVisible(true)]
public System.Reflection.Emit.ConstructorBuilder DefineConstructor(System.Reflection.MethodAttributes attributes, System.Reflection.CallingConventions callingConvention, Type[] parameterTypes, Type[][] requiredCustomModifiers, Type[][] optionalCustomModifiers);

Parameters

attributes
MethodAttributes

The attributes of the constructor.

callingConvention
CallingConventions

The calling convention of the constructor.

parameterTypes
Type[]

The parameter types of the constructor.

requiredCustomModifiers
Type[][]

An array of arrays of types. Each array of types represents the required custom modifiers for the corresponding parameter, such as IsConst. If a particular parameter has no required custom modifiers, specify null instead of an array of types. If none of the parameters have required custom modifiers, specify null instead of an array of arrays.

optionalCustomModifiers
Type[][]

An array of arrays of types. Each array of types represents the optional custom modifiers for the corresponding parameter, such as IsConst. If a particular parameter has no optional custom modifiers, specify null instead of an array of types. If none of the parameters have optional custom modifiers, specify null instead of an array of arrays.

Returns

The defined constructor.

Attributes

Exceptions

The size of requiredCustomModifiers or optionalCustomModifiers does not equal the size of parameterTypes.

The type was previously created using CreateType().

-or-

For the current dynamic type, the IsGenericType property is true, but the IsGenericTypeDefinition property is false.

Remarks

This overload is provided for designers of managed compilers.

Applies to

.NET 9 and other versions
Product Versions
.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 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