TypeBuilder.DefineTypeInitializer Method

Defines the initializer for this type.

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

public ConstructorBuilder DefineTypeInitializer()

Return Value

Type: System.Reflection.Emit.ConstructorBuilder
A type initializer.


The containing type has been previously created using CreateType.

The created initializer is always public.

The following example demonstrates how to create an initialization constructor using DefineTypeInitializer.

using System.Reflection;
using System;
using System.Reflection.Emit;

public class Example
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
      AssemblyName myAssemblyName = new AssemblyName("EmittedAssembly");
      AssemblyBuilder myAssembly = 
      ModuleBuilder myModule = myAssembly.DefineDynamicModule("EmittedModule");

      // Define a public class named "HelloWorld" in the assembly. By default,
      // this class has a public parameterless instance constructor, so there
      // is no need to create one.
      TypeBuilder helloWorldClass = myModule.DefineType("HelloWorld", TypeAttributes.Public);

      // Define a private Shared String field named "Greeting" in the type.
      FieldBuilder greetingField = 
         helloWorldClass.DefineField("Greeting", typeof(string), 
                                     FieldAttributes.Private | FieldAttributes.Static);

      // Create the type initializer (class constructor).
      ConstructorBuilder cctor = helloWorldClass.DefineTypeInitializer();

      // Generate IL for the type initializer, which initializes the private 
      // field.
      ILGenerator il = cctor.GetILGenerator();

      il.Emit(OpCodes.Ldstr, "Hello, {0}!");
      il.Emit(OpCodes.Stsfld, greetingField);

      // Create an instance method named Greetings, which takes a string
      // argument and returns a string.
      MethodBuilder greetings = 
         helloWorldClass.DefineMethod("Greetings", MethodAttributes.Public, 
                                      typeof(string), new Type[] { typeof(string) });

      // Generate IL for the method, which uses an overload of the String.Format
      // method to insert the string argument into the string in greetingField.
      // Thus, any instance of HelloWorld can create formatted greetings as soon
      // as it is instantiated.
      il = greetings.GetILGenerator();

      il.Emit(OpCodes.Ldsfld, greetingField);
      il.Emit(OpCodes.Call, typeof(string).GetMethod("Format", 
                             new Type[] { typeof(string), typeof(object) }));

      // Create the type and display its constructors. The type initializer is
      // always named .cctor.
      Type t = helloWorldClass.CreateType();

      outputBlock.Text += "Type name: " + t.Name + "\n";
      outputBlock.Text += "Constructors:\n";
      foreach(ConstructorInfo ctor in t.GetConstructors(BindingFlags.Public | 
                BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance))
         outputBlock.Text += "   " + ctor.ToString() + "\n";

      // Create an instance of HelloWorld, and generate a greeting.
      object obj = Activator.CreateInstance(t);
      outputBlock.Text += "\n" + t.InvokeMember("Greetings", 
                                                new object[] { "World" }) + "\n";

/* This code produces the following output:

Type name: HelloWorld
    Void .cctor()
    Void .ctor()

Hello, World!


Supported in: 5, 4, 3

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.