Share via


Defining a Constructor with Reflection Emit 

A constructor is defined using the TypeBuilder.DefineConstructor method. DefineConstructor returns a ConstructorBuilder. DefineConstructor requires the caller to specify the constructor attributes using the MethodAttributes enumeration.

The default constructor for a class is defined using the TypeBuilder.DefineDefaultConstructor method. DefineDefaultConstructor returns a ConstructorBuilder. The default constructor simply calls the constructor of the parent class. The common language runtime automatically defines a default constructor for a class if the caller does not do so.

Attributes

  • The runtime sets the attribute MethodAttributes.SpecialName for the constructor.

  • Private constructors are specified using the MethodAttributes.Private attribute. For other visibility attributes, see the description of the MethodAttributes enumeration.

Known Issues

  • Although ConstructorBuilder is derived from ConstructorInfo, some of the abstract methods defined in the ConstructorInfo class are not fully implemented in ConstructorBuilder. These ConstructorBuilder methods throw the NotSupportedException. The desired functionality can be obtained by retrieving the type containing the constructor and reflecting on it. For example the ConstructorBuilder.Invoke method is not fully implemented.

  • Custom modifiers are not currently supported.

See Also

Other Resources

Using Reflection Emit