Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

TypeBuilder.DefineConstructor Method

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

[Visual Basic]
Public Function DefineConstructor( _
   ByVal attributes As MethodAttributes, _
   ByVal callingConvention As CallingConventions, _
   ByVal parameterTypes() As Type _
) As ConstructorBuilder
[C#]
public ConstructorBuilder DefineConstructor(
 MethodAttributes attributes,
 CallingConventions callingConvention,
 Type[] parameterTypes
);
[C++]
public: ConstructorBuilder* DefineConstructor(
 MethodAttributes attributes,
 CallingConventions callingConvention,
 Type* parameterTypes[]
);
[JScript]
public function DefineConstructor(
   attributes : MethodAttributes,
 callingConvention : CallingConventions,
 parameterTypes : Type[]
) : ConstructorBuilder;

Parameters

attributes
The attributes of the constructor.
callingConvention
The calling convention of the constructor.
parameterTypes
The types of the parameters of the constructor.

Return Value

The defined constructor.

Exceptions

Exception Type Condition
InvalidOperationException The type was previously created using CreateType.

Example

[Visual Basic, C#, C++] 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 IL population.

[Visual Basic] 
' 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)

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

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

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

See Also

TypeBuilder Class | TypeBuilder Members | System.Reflection.Emit Namespace

Show:
© 2015 Microsoft