Export (0) Print
Expand All

TypeBuilder Class

Defines and creates new instances of classes during runtime.

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

[ClassInterfaceAttribute(ClassInterfaceType.None)] 
[ComVisibleAttribute(true)] 
public sealed class TypeBuilder : Type, _TypeBuilder
/** @attribute ClassInterfaceAttribute(ClassInterfaceType.None) */ 
/** @attribute ComVisibleAttribute(true) */ 
public final class TypeBuilder extends Type implements _TypeBuilder
ClassInterfaceAttribute(ClassInterfaceType.None) 
ComVisibleAttribute(true) 
public final class TypeBuilder extends Type implements _TypeBuilder

NoteNote

The HostProtectionAttribute attribute applied to this class has the following Resources property value: MayLeakOnAbort. The HostProtectionAttribute does not affect desktop applications (which are typically started by double-clicking an icon, typing a command, or entering a URL in a browser). For more information, see the HostProtectionAttribute class or SQL Server Programming and Host Protection Attributes.

TypeBuilder is the root class used to control the creation of dynamic classes in the runtime. TypeBuilder provides a set of routines that are used to define classes, add methods and fields, and create the class inside the runtime. A new TypeBuilder can be created from a dynamic module.

To retrieve a Type object for an incomplete type, use ModuleBuilder.GetType with a string representing the type name, such as "MyType" or "MyType[]".

The following code sample demonstrates how to build a dynamic type using TypeBuilder.


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


class TestILGenerator {
 
  	public static Type DynamicDotProductGen() {
	  
	   Type ivType = null;
	   Type[] ctorParams = new Type[] { typeof(int),
		               		    typeof(int),
					    typeof(int)};
 	
	   AppDomain myDomain = Thread.GetDomain();
	   AssemblyName myAsmName = new AssemblyName();
	   myAsmName.Name = "IntVectorAsm";
	
	   AssemblyBuilder myAsmBuilder = myDomain.DefineDynamicAssembly(
					  myAsmName, 
					  AssemblyBuilderAccess.RunAndSave);

   	   ModuleBuilder IntVectorModule = myAsmBuilder.DefineDynamicModule("IntVectorModule",
									    "Vector.dll");

	   TypeBuilder ivTypeBld = IntVectorModule.DefineType("IntVector",
						              TypeAttributes.Public);

	   FieldBuilder xField = ivTypeBld.DefineField("x", typeof(int),
                                                       FieldAttributes.Private);
	   FieldBuilder yField = ivTypeBld.DefineField("y", typeof(int), 
                                                       FieldAttributes.Private);
	   FieldBuilder zField = ivTypeBld.DefineField("z", typeof(int),
                                                       FieldAttributes.Private);


           Type objType = Type.GetType("System.Object"); 
           ConstructorInfo objCtor = objType.GetConstructor(new Type[0]);

	   ConstructorBuilder ivCtor = ivTypeBld.DefineConstructor(
					  MethodAttributes.Public,
					  CallingConventions.Standard,
					  ctorParams);
	   ILGenerator ctorIL = ivCtor.GetILGenerator();
           ctorIL.Emit(OpCodes.Ldarg_0);
           ctorIL.Emit(OpCodes.Call, objCtor);
           ctorIL.Emit(OpCodes.Ldarg_0);
           ctorIL.Emit(OpCodes.Ldarg_1);
           ctorIL.Emit(OpCodes.Stfld, xField); 
           ctorIL.Emit(OpCodes.Ldarg_0);
           ctorIL.Emit(OpCodes.Ldarg_2);
           ctorIL.Emit(OpCodes.Stfld, yField); 
           ctorIL.Emit(OpCodes.Ldarg_0);
           ctorIL.Emit(OpCodes.Ldarg_3);
           ctorIL.Emit(OpCodes.Stfld, zField); 
	   ctorIL.Emit(OpCodes.Ret); 


	   // This method will find the dot product of the stored vector
	   // with another.

	   Type[] dpParams = new Type[] { ivTypeBld };

           // Here, you create a MethodBuilder containing the
	   // name, the attributes (public, static, private, and so on),
	   // the return type (int, in this case), and a array of Type
	   // indicating the type of each parameter. Since the sole parameter
	   // is a IntVector, the very class you're creating, you will
	   // pass in the TypeBuilder (which is derived from Type) instead of 
	   // a Type object for IntVector, avoiding an exception. 

	   // -- This method would be declared in C# as:
	   //    public int DotProduct(IntVector aVector)

           MethodBuilder dotProductMthd = ivTypeBld.DefineMethod(
	    		                  "DotProduct", 
				          MethodAttributes.Public,
                                          typeof(int), 
                                          dpParams);

	   // A ILGenerator can now be spawned, attached to the MethodBuilder.

	   ILGenerator mthdIL = dotProductMthd.GetILGenerator();
	   
 	   // Here's the body of our function, in MSIL form. We're going to find the
	   // "dot product" of the current vector instance with the passed vector 
	   // instance. For reference purposes, the equation is:
	   // (x1 * x2) + (y1 * y2) + (z1 * z2) = the dot product

	   // First, you'll load the reference to the current instance "this"
	   // stored in argument 0 (ldarg.0) onto the stack. Ldfld, the subsequent
	   // instruction, will pop the reference off the stack and look up the
	   // field "x", specified by the FieldInfo token "xField".

	   mthdIL.Emit(OpCodes.Ldarg_0);
	   mthdIL.Emit(OpCodes.Ldfld, xField);

	   // That completed, the value stored at field "x" is now atop the stack.
	   // Now, you'll do the same for the object reference we passed as a
	   // parameter, stored in argument 1 (ldarg.1). After Ldfld executed,
	   // you'll have the value stored in field "x" for the passed instance
	   // atop the stack.

	   mthdIL.Emit(OpCodes.Ldarg_1);
	   mthdIL.Emit(OpCodes.Ldfld, xField);

           // There will now be two values atop the stack - the "x" value for the
	   // current vector instance, and the "x" value for the passed instance.
	   // You'll now multiply them, and push the result onto the evaluation stack.

	   mthdIL.Emit(OpCodes.Mul_Ovf_Un);

	   // Now, repeat this for the "y" fields of both vectors.

	   mthdIL.Emit(OpCodes.Ldarg_0);
	   mthdIL.Emit(OpCodes.Ldfld, yField);
	   mthdIL.Emit(OpCodes.Ldarg_1);
	   mthdIL.Emit(OpCodes.Ldfld, yField);
	   mthdIL.Emit(OpCodes.Mul_Ovf_Un);

	   // At this time, the results of both multiplications should be atop
	   // the stack. You'll now add them and push the result onto the stack.

	   mthdIL.Emit(OpCodes.Add_Ovf_Un);

	   // Multiply both "z" field and push the result onto the stack.
	   mthdIL.Emit(OpCodes.Ldarg_0);
	   mthdIL.Emit(OpCodes.Ldfld, zField);
	   mthdIL.Emit(OpCodes.Ldarg_1);
	   mthdIL.Emit(OpCodes.Ldfld, zField);
	   mthdIL.Emit(OpCodes.Mul_Ovf_Un);

	   // Finally, add the result of multiplying the "z" fields with the
	   // result of the earlier addition, and push the result - the dot product -
	   // onto the stack.
	   mthdIL.Emit(OpCodes.Add_Ovf_Un);

	   // The "ret" opcode will pop the last value from the stack and return it
	   // to the calling method. You're all done!

	   mthdIL.Emit(OpCodes.Ret);


 	   ivType = ivTypeBld.CreateType();

	   return ivType;

 	}

	public static void Main() {
	
	   Type IVType = null;
           object aVector1 = null;
           object aVector2 = null;
	   Type[] aVtypes = new Type[] {typeof(int), typeof(int), typeof(int)};
           object[] aVargs1 = new object[] {10, 10, 10};
           object[] aVargs2 = new object[] {20, 20, 20};
	
	   // Call the  method to build our dynamic class.

	   IVType = DynamicDotProductGen();

           Console.WriteLine("---");

	   ConstructorInfo myDTctor = IVType.GetConstructor(aVtypes);
	   aVector1 = myDTctor.Invoke(aVargs1);
	   aVector2 = myDTctor.Invoke(aVargs2);

	   object[] passMe = new object[1];
           passMe[0] = (object)aVector2; 

	   Console.WriteLine("(10, 10, 10) . (20, 20, 20) = {0}",
			     IVType.InvokeMember("DotProduct",
						  BindingFlags.InvokeMethod,
						  null,
						  aVector1,
						  passMe));

	    

	   // +++ OUTPUT +++
	   // ---
	   // (10, 10, 10) . (20, 20, 20) = 600 
	    
	}
    
}


import System.*;
import System.Threading.*;
import System.Reflection.*;
import System.Reflection.Emit.*;

class TestILGenerator
{
   public static Type DynamicDotProductGen() 
   {
        Type ivType = null;
        Type ctorParams[] = new Type[]{int.class.ToType(),
            int.class.ToType(), int.class.ToType()};

        AppDomain myDomain = System.Threading.Thread.GetDomain();
        AssemblyName myAsmName =  new AssemblyName();
        myAsmName.set_Name("IntVectorAsm");

        AssemblyBuilder myAsmBuilder = myDomain.DefineDynamicAssembly
            (myAsmName, AssemblyBuilderAccess.RunAndSave);

        ModuleBuilder IntVectorModule = myAsmBuilder.DefineDynamicModule
            ("IntVectorModule", "Vector.dll");

        TypeBuilder ivTypeBld = IntVectorModule.DefineType("IntVector",
            TypeAttributes.Public);

        FieldBuilder xField = ivTypeBld.DefineField("x",
            int.class.ToType(), FieldAttributes.Private);
        FieldBuilder yField = ivTypeBld.DefineField("y",
            int.class.ToType(), FieldAttributes.Private);
        FieldBuilder zField = ivTypeBld.DefineField("z",
            int.class.ToType(), FieldAttributes.Private);

        Type objType = Type.GetType("System.Object");
        ConstructorInfo objCtor = objType.GetConstructor(new Type[0]);
        ConstructorBuilder ivCtor = 
            ivTypeBld.DefineConstructor(MethodAttributes.Public,
            CallingConventions.Standard, ctorParams);

        ILGenerator ctorIL = ivCtor.GetILGenerator();

        ctorIL.Emit(OpCodes.Ldarg_0);
        ctorIL.Emit(OpCodes.Call, objCtor);
        ctorIL.Emit(OpCodes.Ldarg_0);
        ctorIL.Emit(OpCodes.Ldarg_1);
        ctorIL.Emit(OpCodes.Stfld, xField);
        ctorIL.Emit(OpCodes.Ldarg_0);
        ctorIL.Emit(OpCodes.Ldarg_2);
        ctorIL.Emit(OpCodes.Stfld, yField);
        ctorIL.Emit(OpCodes.Ldarg_0);
        ctorIL.Emit(OpCodes.Ldarg_3);
        ctorIL.Emit(OpCodes.Stfld, zField);
        ctorIL.Emit(OpCodes.Ret);
      
        // This method will find the dot product of the stored vector
        // with another.
        Type dpParams[] = new Type[]{ivTypeBld};
              
        // Here, you create a MethodBuilder containing the
        // name, the attributes (public, static, private, and so on),
        // the return type (int, in this case), and a array of Type
        // indicating the type of each parameter. Since the sole parameter
        // is a IntVector, the very class you're creating, you will
        // pass in the TypeBuilder (which is derived from Type) instead of 
        // a Type object for IntVector, avoiding an exception. 
        // -- This method would be declared in VJ# as:
        //    public int DotProduct(IntVector aVector)
        MethodBuilder dotProductMthd = ivTypeBld.DefineMethod("DotProduct",
            MethodAttributes.Public, int .class.ToType(), dpParams);
              
        // A ILGenerator can now be spawned, attached to the MethodBuilder.
        ILGenerator mthdIL = dotProductMthd.GetILGenerator();
              
        // Here's the body of our function, in MSIL form. We're going to 
        // find the "dot product" of the current vector instance with the 
        // passed vector instance. For reference purposes, the equation is:
        // (x1 * x2) + (y1 * y2) + (z1 * z2) = the dot product
        // First, you'll load the reference to the current instance "this"
        // stored in argument 0 (ldarg.0) onto the stack. Ldfld, the 
        // subsequent instruction, will pop the reference off the stack and 
        // look up the field "x",specified by the FieldInfo token "xField".
        mthdIL.Emit(OpCodes.Ldarg_0);
        mthdIL.Emit(OpCodes.Ldfld, xField);
      
        // That completed, the value stored at field "x" is now atop the 
        // stack.Now, you'll do the same for the object reference we passed 
        // as a parameter, stored in argument 1 (ldarg.1). After Ldfld 
        // executed,you'll have the value stored in field "x" for the 
        // passed instance atop the stack.
        mthdIL.Emit(OpCodes.Ldarg_1);
        mthdIL.Emit(OpCodes.Ldfld, xField);
              
        // There will now be two values atop the stack - the "x" value for 
        // the current vector instance, and the "x" value for the passed 
        // instance.You'll now multiply them, and push the result onto the
        // evaluation stack.
        mthdIL.Emit(OpCodes.Mul_Ovf_Un);
            
        // Now, repeat this for the "y" fields of both vectors.
        mthdIL.Emit(OpCodes.Ldarg_0);
        mthdIL.Emit(OpCodes.Ldfld, yField);
        mthdIL.Emit(OpCodes.Ldarg_1);
        mthdIL.Emit(OpCodes.Ldfld, yField);
        mthdIL.Emit(OpCodes.Mul_Ovf_Un);
            
        // At this time, the results of both multiplications should be atop
        // the stack. You'll now add them and push the result
        // onto the stack.
        mthdIL.Emit(OpCodes.Add_Ovf_Un);
            
        // Multiply both "z" field and push the result onto the stack.
        mthdIL.Emit(OpCodes.Ldarg_0);
        mthdIL.Emit(OpCodes.Ldfld, zField);
        mthdIL.Emit(OpCodes.Ldarg_1);
        mthdIL.Emit(OpCodes.Ldfld, zField);
        mthdIL.Emit(OpCodes.Mul_Ovf_Un);
            
        // Finally, add the result of multiplying the "z" fields with the
        // result of the earlier addition, and push the result 
        // - the dot product - onto the stack.
        mthdIL.Emit(OpCodes.Add_Ovf_Un);
        // The "ret" opcode will pop the last value from the stack and 
        // return it to the calling method. You're all done!
        mthdIL.Emit(OpCodes.Ret);
        ivType = ivTypeBld.CreateType();
        return ivType ;
   } //DynamicDotProductGen
     
    public static void main(String[] args)
    {
        Type ivType = null;
        Object aVector1 = null;
        Object aVector2 = null;
        Type aVtypes[] = new Type[] {
            int.class.ToType(), int.class.ToType(), int.class.ToType()};
        Object aVargs1[] = new Object[] { (Int32)10, (Int32)10, (Int32)10};
        Object aVargs2[] = new Object[] { (Int32)20, (Int32)20, (Int32)20};

        // Call the  method to build our dynamic class.
        ivType = DynamicDotProductGen();
        Console.WriteLine("---");
        ConstructorInfo myDTctor = ivType.GetConstructor(aVtypes);
        aVector1 = myDTctor.Invoke(aVargs1);
        aVector2 = myDTctor.Invoke(aVargs2);
        Object passMe[] = new Object[1];
        passMe.set_Item(0, ((Object)(aVector2)));
        Console.WriteLine("(10, 10, 10) . (20, 20, 20) = {0}",
            ivType.InvokeMember("DotProduct", BindingFlags.InvokeMethod,
            null, aVector1, passMe));
    } //main
} //TestILGenerator
// +++ OUTPUT +++
// ---
// (10, 10, 10) . (20, 20, 20) = 600 

System.Object
   System.Reflection.MemberInfo
     System.Type
      System.Reflection.Emit.TypeBuilder

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 98, Windows 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 .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft