TypeBuilder.DefineConstructor TypeBuilder.DefineConstructor TypeBuilder.DefineConstructor TypeBuilder.DefineConstructor Method

Definition

Adds a new constructor to the dynamic type.

Overloads

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

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

DefineConstructor(MethodAttributes, CallingConventions, Type[], Type[][], Type[][]) DefineConstructor(MethodAttributes, CallingConventions, Type[], Type[][], Type[][]) DefineConstructor(MethodAttributes, CallingConventions, Type[], Type[][], Type[][]) 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[]) DefineConstructor(MethodAttributes, CallingConventions, Type[]) DefineConstructor(MethodAttributes, CallingConventions, Type[]) DefineConstructor(MethodAttributes, CallingConventions, Type[])

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

public:
 System::Reflection::Emit::ConstructorBuilder ^ DefineConstructor(System::Reflection::MethodAttributes attributes, System::Reflection::CallingConventions callingConvention, cli::array <Type ^> ^ parameterTypes);
[System.Runtime.InteropServices.ComVisible(true)]
public System.Reflection.Emit.ConstructorBuilder DefineConstructor (System.Reflection.MethodAttributes attributes, System.Reflection.CallingConventions callingConvention, Type[] parameterTypes);
member this.DefineConstructor : System.Reflection.MethodAttributes * System.Reflection.CallingConventions * Type[] -> System.Reflection.Emit.ConstructorBuilder
Public Function DefineConstructor (attributes As MethodAttributes, callingConvention As CallingConventions, parameterTypes As Type()) As ConstructorBuilder

Parameters

attributes
MethodAttributes MethodAttributes MethodAttributes MethodAttributes

The attributes of the constructor.

callingConvention
CallingConventions CallingConventions CallingConventions CallingConventions

The calling convention of the constructor.

parameterTypes
Type[]

The parameter types of the constructor.

Returns

Exceptions

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.

// Define the constructor.
array<Type^>^ constructorArgs = {String::typeid};
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 );
// 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);
' Define the constructor.
Dim constructorArgs As Type() = {GetType(String)}
Dim myConstructorBuilder As ConstructorBuilder = helloWorldTypeBuilder.DefineConstructor _
                     (MethodAttributes.Public, CallingConventions.Standard, constructorArgs)
' Generate IL for the method.The constructor stores its argument in the private field.
Dim myConstructorIL As ILGenerator = 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 default constructor is provided automatically, and it calls the default constructor of the base class.

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

  • If you want a default constructor that simply calls the default 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 default 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 default constructor that does something more than simply calling the default 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.

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

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

public:
 System::Reflection::Emit::ConstructorBuilder ^ DefineConstructor(System::Reflection::MethodAttributes attributes, System::Reflection::CallingConventions callingConvention, cli::array <Type ^> ^ parameterTypes, cli::array <cli::array <Type ^> ^> ^ requiredCustomModifiers, cli::array <cli::array <Type ^> ^> ^ optionalCustomModifiers);
[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);
member this.DefineConstructor : System.Reflection.MethodAttributes * System.Reflection.CallingConventions * Type[] * Type[][] * Type[][] -> System.Reflection.Emit.ConstructorBuilder
Public Function DefineConstructor (attributes As MethodAttributes, callingConvention As CallingConventions, parameterTypes As Type(), requiredCustomModifiers As Type()(), optionalCustomModifiers As Type()()) As ConstructorBuilder

Parameters

attributes
MethodAttributes MethodAttributes MethodAttributes MethodAttributes

The attributes of the constructor.

callingConvention
CallingConventions CallingConventions CallingConventions 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

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.

Note

For more information on custom modifiers, see the ECMA Partition II Metadata documentation. The documentation is available online; see ECMA C# and Common Language Infrastructure Standards on MSDN and Standard ECMA-335 - Common Language Infrastructure (CLI) on the Ecma International Web site.

Applies to