Upraviť

Zdieľať cez


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.

public:
 System::Reflection::Emit::ConstructorBuilder ^ DefineConstructor(System::Reflection::MethodAttributes attributes, System::Reflection::CallingConventions callingConvention, cli::array <Type ^> ^ parameterTypes);
public System.Reflection.Emit.ConstructorBuilder DefineConstructor (System.Reflection.MethodAttributes attributes, System.Reflection.CallingConventions callingConvention, Type[]? parameterTypes);
public System.Reflection.Emit.ConstructorBuilder DefineConstructor (System.Reflection.MethodAttributes attributes, System.Reflection.CallingConventions callingConvention, 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
[<System.Runtime.InteropServices.ComVisible(true)>]
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

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.

// 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 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

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.

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);
public System.Reflection.Emit.ConstructorBuilder DefineConstructor (System.Reflection.MethodAttributes attributes, System.Reflection.CallingConventions callingConvention, Type[]? parameterTypes, Type[][]? requiredCustomModifiers, Type[][]? optionalCustomModifiers);
public System.Reflection.Emit.ConstructorBuilder DefineConstructor (System.Reflection.MethodAttributes attributes, System.Reflection.CallingConventions callingConvention, Type[] parameterTypes, Type[][] requiredCustomModifiers, 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
[<System.Runtime.InteropServices.ComVisible(true)>]
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

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