Export (0) Print
Expand All

TypeBuilder.DefineDefaultConstructor Method

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

Namespace:  System.Reflection.Emit
Assembly:  mscorlib (in mscorlib.dll)

<ComVisibleAttribute(True)> _
Public Function DefineDefaultConstructor ( _
	attributes As MethodAttributes _
) As ConstructorBuilder


Type: System.Reflection.MethodAttributes

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

Return Value

Type: System.Reflection.Emit.ConstructorBuilder
Returns the constructor.


The parent type (base type) does not have a default constructor.


The type was previously created using CreateType.


For the current dynamic type, the IsGenericType property is true, but the IsGenericTypeDefinition property is false.

Because the default constructor is automatically defined, it is necessary to call this method only in the following situations:

  • You have defined another constructor and you also want a default constructor that simply calls the base class constructor.

  • You want to set the attributes on the default constructor to something other than PrivateScope, Public, HideBySig, SpecialName, and RTSpecialName.

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. 
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.Stfld, myGreetingField)

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1

.NET Framework Client Profile

Supported in: 4, 3.5 SP1
© 2015 Microsoft