This documentation is archived and is not being maintained.

TypeBuilder.DefineDefaultConstructor Method

Defines the default constructor. The constructor defined here will simply call the default constructor of the parent.

[Visual Basic]
Public Function DefineDefaultConstructor( _
   ByVal attributes As MethodAttributes _
) As ConstructorBuilder
[C#]
public ConstructorBuilder DefineDefaultConstructor(
 MethodAttributes attributes
);
[C++]
public: ConstructorBuilder* DefineDefaultConstructor(
 MethodAttributes attributes
);
[JScript]
public function DefineDefaultConstructor(
   attributes : MethodAttributes
) : ConstructorBuilder;

Parameters

attributes
A MethodAttributes object representing the attributes to be applied to the constructor.

Return Value

Returns the constructor.

Exceptions

Exception Type Condition
NotSupportedException The parent class does not have a default constructor

Remarks

Since the default constructor is automatically defined, it is only necessary to call this method if the attributes on the default constructor should be set to something other than MethodBase.Constructor. This method is provided here to make it easier to set the attributes.

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: