Export (0) Print
Expand All

TypeBuilder.DefineTypeInitializer Method

Defines the initializer for this type.

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

[ComVisibleAttribute(true)] 
public ConstructorBuilder DefineTypeInitializer ()
/** @attribute ComVisibleAttribute(true) */ 
public ConstructorBuilder DefineTypeInitializer ()
ComVisibleAttribute(true) 
public function DefineTypeInitializer () : ConstructorBuilder
Not applicable.

Return Value

Returns a type initializer.

Exception typeCondition

InvalidOperationException

The containing type has been previously created using CreateType.

The initializer created is always public.

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

public class MyApplication
{
   public static void Main()
   {
      // Create the "HelloWorld" class
      TypeBuilder helloWorldClass = CreateCallee(Thread.GetDomain());
      Console.WriteLine("Full Name : " + helloWorldClass.FullName);
      Console.WriteLine("Constructors :");
      ConstructorInfo[] info =
         helloWorldClass.GetConstructors(BindingFlags.Public|BindingFlags.Instance);
      for(int index=0; index < info.Length; index++)
         Console.WriteLine(info[index].ToString());
   }

   // Create the callee transient dynamic assembly.
   private static TypeBuilder CreateCallee(AppDomain myDomain)
   {
      AssemblyName myAssemblyName = new AssemblyName();
      myAssemblyName.Name = "EmittedAssembly";

      // Create the callee dynamic assembly.
      AssemblyBuilder myAssembly = myDomain.DefineDynamicAssembly(myAssemblyName,
         AssemblyBuilderAccess.Run);
      // Create a dynamic module named "CalleeModule" in the callee assembly.
      ModuleBuilder myModule = myAssembly.DefineDynamicModule("EmittedModule");

      // Define a public class named "HelloWorld" in the assembly.
      TypeBuilder helloWorldClass = myModule.DefineType("HelloWorld", TypeAttributes.Public);
      // Define a private String field named "Greeting" in the type.
      FieldBuilder greetingField = helloWorldClass.DefineField("Greeting", typeof(String),
         FieldAttributes.Private);

      // Create the constructor.
      ConstructorBuilder constructor = helloWorldClass.DefineTypeInitializer();

      // Generate IL for the method. The constructor calls its base class
      // constructor. The constructor stores its argument in the private field.
      ILGenerator constructorIL = constructor.GetILGenerator();
      constructorIL.Emit(OpCodes.Ldarg_0);
      ConstructorInfo superConstructor = typeof(Object).GetConstructor(new Type[0]);
      constructorIL.Emit(OpCodes.Call, superConstructor);
      constructorIL.Emit(OpCodes.Ldarg_0);
      constructorIL.Emit(OpCodes.Ldarg_1);
      constructorIL.Emit(OpCodes.Stfld, greetingField);
      constructorIL.Emit(OpCodes.Ret);

      helloWorldClass.CreateType();
      return(helloWorldClass);
   }
}

public class MyApplication
{
    public static void main(String[] args)
    {
        // Create the "HelloWorld" class
        TypeBuilder helloWorldClass =
            CreateCallee(System.Threading.Thread.GetDomain());
        Console.WriteLine("Full Name : " + helloWorldClass.get_FullName());
        Console.WriteLine("Constructors :");
        ConstructorInfo info[] =
            helloWorldClass.GetConstructors(BindingFlags.Public
            | BindingFlags.Instance);
        for (int index = 0; index < info.length; index++) {
            Console.WriteLine(info.get_Item(index).ToString());
        }
    } //main

    // Create the callee transient dynamic assembly.
    private static TypeBuilder CreateCallee(AppDomain myDomain)
    {
        AssemblyName myAssemblyName = new AssemblyName();
        myAssemblyName.set_Name("EmittedAssembly");
        // Create the callee dynamic assembly.
        AssemblyBuilder myAssembly = myDomain.DefineDynamicAssembly(
            myAssemblyName, AssemblyBuilderAccess.Run);
        // Create a dynamic module named "CalleeModule" in the callee assembly.
        ModuleBuilder myModule =
            myAssembly.DefineDynamicModule("EmittedModule");
        // Define a public class named "HelloWorld" in the assembly.
        TypeBuilder helloWorldClass = myModule.DefineType("HelloWorld",
            TypeAttributes.Public);
        // Define a private String field named "Greeting" in the type.
        FieldBuilder greetingField = helloWorldClass.DefineField("Greeting",
            String.class.ToType(), FieldAttributes.Private);
        // Create the constructor.
        ConstructorBuilder constructor = helloWorldClass.DefineTypeInitializer();
        // Generate IL for the method. The constructor calls its base class
        // constructor. The constructor stores its argument in the private field.
        ILGenerator constructorIL = constructor.GetILGenerator();
        constructorIL.Emit(OpCodes.Ldarg_0);
        ConstructorInfo superConstructor = Object.class.ToType().
            GetConstructor(new Type[0]);
        constructorIL.Emit(OpCodes.Call, superConstructor);
        constructorIL.Emit(OpCodes.Ldarg_0);
        constructorIL.Emit(OpCodes.Ldarg_1);
        constructorIL.Emit(OpCodes.Stfld, greetingField);
        constructorIL.Emit(OpCodes.Ret);

        helloWorldClass.CreateType();
        return helloWorldClass;
    } //CreateCallee
} //MyApplication

Windows 98, Windows Server 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft