Defining a Constructor with Reflection Emit
Important This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here. ArchiveDisclaimer

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.

  • 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.

  • 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.

© 2016 Microsoft