.NET API Reference

ConstructorBuilder Class

public sealed class ConstructorBuilder : MethodBase

Represents a constructor during dynamic assembly creation. This class cannot be inherited. It provides methods to define the structure and IL for a constructor of a dynamically generated type.

Summary

Properties

  • MethodImplementationFlags

    Gets the implementation flags for the method.

  • Name

    Gets the name of the member.

  • CallingConvention

    Gets the calling convention of the method.

  • IsPublic

    Gets a value indicating whether the method has public accessibility.

  • IsPrivate

    Gets a value indicating whether the method has private accessibility.

  • IsFamily

    Gets a value indicating whether the method has family (protected) accessibility.

  • IsFamilyAndAssembly

    Gets a value indicating whether the method has family and assembly accessibility.

  • IsFamilyOrAssembly

    Gets a value indicating whether the method has family or assembly accessibility.

  • IsAssembly

    Gets a value indicating whether the method has assembly accessibility.

  • IsStatic

    Gets a value indicating whether the method is static.

  • IsFinal

    Gets a value indicating whether the method is final.

  • IsVirtual

    Gets a value indicating whether the method is virtual.

  • IsSpecialName

    Gets a value indicating whether the method is a special name.

  • IsGenericMethod

    Gets a value indicating whether the method is a generic method definition.

  • IsGenericMethodDefinition

    Gets a value indicating whether the current method is a generic method definition.

  • IsGenericArgumentsDefault

    Gets a value indicating whether the generic type arguments are default.

  • ContainsGenericParameters

    Gets a value indicating whether the method contains generic parameters.

  • ReturnParameter

    Gets the return parameter of the method.

  • MethodReturnClassification

    Gets the classification of the method's return value.

Methods

Implements

Remarks

Use the TypeBuilder class to create a new type. Then, use the DefineConstructor method of the TypeBuilder to get a ConstructorBuilder object. You can then use the CreateILGenerator method to get an ILGenerator object, which is used to emit the intermediate language (IL) instructions for the constructor.

Example

using System; using System.Reflection; using System.Reflection.Emit; public class Example { public static void Main() { // Create a dynamic assembly AssemblyName assemblyName = new AssemblyName("MyDynamicAssembly"); AssemblyBuilder assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run); // Create a module in the assembly ModuleBuilder moduleBuilder = assemblyBuilder.DefineDynamicModule("MyDynamicModule"); // Define a type TypeBuilder typeBuilder = moduleBuilder.DefineType("MyDynamicClass", TypeAttributes.Public); // Define a constructor with no parameters ConstructorBuilder constructorBuilder = typeBuilder.DefineDefaultConstructor(MethodAttributes.Public); // Get an ILGenerator and emit instructions (optional, for custom constructors) ILGenerator ilGenerator = constructorBuilder.GetILGenerator(); ilGenerator.Emit(OpCodes.Ret); // Return instruction // Create the type Type myType = typeBuilder.CreateType(); // Get the constructor ConstructorInfo constructorInfo = myType.GetConstructor(Type.EmptyTypes); // Create an instance of the dynamic type object instance = constructorInfo.Invoke(null); Console.WriteLine($"Instance created: {instance.GetType().Name}"); } }

Requirements