TypeBuilder::DefineConstructor Method (MethodAttributes, CallingConventions, array<Type^>^)
Adds a new constructor to the type, with the given attributes and signature.
Assembly: mscorlib (in mscorlib.dll)
public: [ComVisibleAttribute(true)] ConstructorBuilder^ DefineConstructor( MethodAttributes attributes, CallingConventions callingConvention, array<Type^>^ parameterTypes )
Parameters
- attributes
-
Type:
System.Reflection::MethodAttributes
The attributes of the constructor.
- callingConvention
-
Type:
System.Reflection::CallingConventions
The calling convention of the constructor.
- parameterTypes
-
Type:
array<System::Type^>^
The parameter types of the constructor.
| Exception | Condition |
|---|---|
| InvalidOperationException | The type was previously created using CreateType. |
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.
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 );
Available since 1.1
Silverlight
Available since 2.0