Export (0) Print
Expand All

OpCodes Class

Provides field representations of the Microsoft Intermediate Language (MSIL) instructions for emission by the ILGenerator class members (such as Emit).

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

[ComVisibleAttribute(true)] 
public class OpCodes
/** @attribute ComVisibleAttribute(true) */ 
public class OpCodes
ComVisibleAttribute(true) 
public class OpCodes

For a detailed description of the member opcodes, see the Common Language Infrastructure (CLI) documentation, especially "Partition III: CIL Instruction Set" and "Partition II: Metadata Definition and Semantics". The documentation is available online at http://msdn.microsoft.com/net/ecma/ and http://www.ecma-international.org/publications/standards/Ecma-335.htm.

The following example demonstrates the construction of a dynamic method using ILGenerator to emit OpCodes into a MethodBuilder.


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

class EmitWriteLineDemo {

   public static Type CreateDynamicType() {       
       Type[] ctorParams = new Type[] {typeof(int),
				   typeof(int)};
 	
       AppDomain myDomain = Thread.GetDomain();
       AssemblyName myAsmName = new AssemblyName();
       myAsmName.Name = "MyDynamicAssembly";

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

       ModuleBuilder pointModule = myAsmBuilder.DefineDynamicModule("PointModule",
								    "Point.dll");

       TypeBuilder pointTypeBld = pointModule.DefineType("Point",
					              TypeAttributes.Public);

       FieldBuilder xField = pointTypeBld.DefineField("x", typeof(int),
                                                      FieldAttributes.Public);
       FieldBuilder yField = pointTypeBld.DefineField("y", typeof(int), 
                                                      FieldAttributes.Public);


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

       ConstructorBuilder pointCtor = pointTypeBld.DefineConstructor(
 				                   MethodAttributes.Public,
				                   CallingConventions.Standard,
				                   ctorParams);
       ILGenerator ctorIL = pointCtor.GetILGenerator();


       // First, you build the constructor.
       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.Ret); 

       //  Now, you'll build a method to output some information on the
       // inside your dynamic class. This method will have the following
       // definition in C#:
	//  public void WritePoint()
      
       MethodBuilder writeStrMthd = pointTypeBld.DefineMethod(
        		                     "WritePoint", 
				             MethodAttributes.Public,
                                             typeof(void), 
                                             null);

       
       ILGenerator writeStrIL = writeStrMthd.GetILGenerator();
      
       // The below ILGenerator created demonstrates a few ways to create
       // string output through STDIN. 

       // ILGenerator.EmitWriteLine(string) will generate a ldstr and a 
       // call to WriteLine for you.

       writeStrIL.EmitWriteLine("The value of this current instance is:");

       // Here, you will do the hard work yourself. First, you need to create
       // the string we will be passing and obtain the correct WriteLine overload
       // for said string. In the below case, you are substituting in two values,
       // so the chosen overload is Console.WriteLine(string, object, object).

       String inStr = "({0}, {1})";
       Type[] wlParams = new Type[] {typeof(string),
				     typeof(object),
				     typeof(object)};

       // We need the MethodInfo to pass into EmitCall later.

       MethodInfo writeLineMI = typeof(Console).GetMethod(
					        "WriteLine",
						wlParams);

       // Push the string with the substitutions onto the stack.
       // This is the first argument for WriteLine - the string one. 

       writeStrIL.Emit(OpCodes.Ldstr, inStr);

       // Since the second argument is an object, and it corresponds to
       // to the substitution for the value of our integer field, you 
       // need to box that field to an object. First, push a reference
       // to the current instance, and then push the value stored in
       // field 'x'. We need the reference to the current instance (stored
       // in local argument index 0) so Ldfld can load from the correct
       // instance (this one).

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

       // Now, we execute the box opcode, which pops the value of field 'x',
       // returning a reference to the integer value boxed as an object.

       writeStrIL.Emit(OpCodes.Box, typeof(int));

       // Atop the stack, you'll find our string inStr, followed by a reference
       // to the boxed value of 'x'. Now, you need to likewise box field 'y'.

       writeStrIL.Emit(OpCodes.Ldarg_0);
       writeStrIL.Emit(OpCodes.Ldfld, yField);
       writeStrIL.Emit(OpCodes.Box, typeof(int));

       // Now, you have all of the arguments for your call to
       // Console.WriteLine(string, object, object) atop the stack:
       // the string InStr, a reference to the boxed value of 'x', and
       // a reference to the boxed value of 'y'.

       // Call Console.WriteLine(string, object, object) with EmitCall.

       writeStrIL.EmitCall(OpCodes.Call, writeLineMI, null);

       // Lastly, EmitWriteLine can also output the value of a field
       // using the overload EmitWriteLine(FieldInfo).

       writeStrIL.EmitWriteLine("The value of 'x' is:");
       writeStrIL.EmitWriteLine(xField);
       writeStrIL.EmitWriteLine("The value of 'y' is:");
       writeStrIL.EmitWriteLine(yField);

       // Since we return no value (void), the the ret opcode will not
       // return the top stack value.

       writeStrIL.Emit(OpCodes.Ret);
      
       return pointTypeBld.CreateType();

   }

   public static void Main() {

      object[] ctorParams = new object[2];

      Console.Write("Enter a integer value for X: "); 
      string myX = Console.ReadLine();
      Console.Write("Enter a integer value for Y: "); 
      string myY = Console.ReadLine();

      Console.WriteLine("---");

      ctorParams[0] = Convert.ToInt32(myX);
      ctorParams[1] = Convert.ToInt32(myY);

      Type ptType = CreateDynamicType();
  
      object ptInstance = Activator.CreateInstance(ptType, ctorParams);
      ptType.InvokeMember("WritePoint",
			  BindingFlags.InvokeMethod,
			  null,
			  ptInstance,
			  new object[0]);
   }
}


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

class EmitWriteLineDemo
{
    public static Type CreateDynamicType()
    {
        Type ctorParams[] = new Type[] { int.class.ToType(), 
            int.class.ToType() };
        AppDomain myDomain = System.Threading.Thread.GetDomain();
        AssemblyName myAsmName = new AssemblyName();
        myAsmName.set_Name("MyDynamicAssembly");
        AssemblyBuilder myAsmBuilder = 
            myDomain.DefineDynamicAssembly(myAsmName,
            AssemblyBuilderAccess.RunAndSave);
        ModuleBuilder pointModule = myAsmBuilder.DefineDynamicModule(
            "PointModule", "Point.dll");
        TypeBuilder pointTypeBld = pointModule.DefineType("Point",
            TypeAttributes.Public);
        FieldBuilder xField = pointTypeBld.DefineField("x", int.class.ToType(),
            FieldAttributes.Public);
        FieldBuilder yField = pointTypeBld.DefineField("y", int.class.ToType(),
            FieldAttributes.Public);
        Type objType = Type.GetType("System.Object");
        ConstructorInfo objCtor = objType.GetConstructor(new Type[0]);
        ConstructorBuilder pointCtor = pointTypeBld.DefineConstructor
            (MethodAttributes.Public, CallingConventions.Standard, ctorParams);
        ILGenerator ctorIL = pointCtor.GetILGenerator();

        // First, you build the constructor.
        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.Ret);

        //  Now, you'll build a method to output some information on the
        // inside your dynamic class. This method will have the following
        // definition in VJ#:
        //  public void WritePoint()
        MethodBuilder writeStrMthd = pointTypeBld.DefineMethod("WritePoint",
            MethodAttributes.Public, void.class.ToType(), null);
        ILGenerator writeStrIL = writeStrMthd.GetILGenerator();

        // The below ILGenerator created demonstrates a few ways to create
        // string output through STDIN. 
        // ILGenerator.EmitWriteLine(string) will generate a ldstr and a 
        // call to WriteLine for you.
        writeStrIL.EmitWriteLine("The value of this current instance is:");

        // Here, you will do the hard work yourself. First, you need to create
        // the string we will be passing and obtain the correct WriteLine
        // overload for said string. In the below case, you are substituting
        // in two values, so the chosen overload is
        // Console.WriteLine(string, object, object).
        String inStr = "({0}, {1})";
        Type wlParams[] = new Type[] { String.class.ToType(),
            Object.class.ToType(), Object.class.ToType() };

        // We need the MethodInfo to pass into EmitCall later.
        MethodInfo writeLineMI = Console.class.ToType().GetMethod("WriteLine",
            wlParams);

        // Push the string with the substitutions onto the stack.
        // This is the first argument for WriteLine - the string one. 
        writeStrIL.Emit(OpCodes.Ldstr, inStr);

        // Since the second argument is an object, and it corresponds to
        // to the substitution for the value of our integer field, you 
        // need to box that field to an object. First, push a reference
        // to the current instance, and then push the value stored in
        // field 'x'. We need the reference to the current instance (stored
        // in local argument index 0) so Ldfld can load from the correct
        // instance (this one).
        writeStrIL.Emit(OpCodes.Ldarg_0);
        writeStrIL.Emit(OpCodes.Ldfld, xField);

        // Now, we execute the box opcode, which pops the value of field 'x',
        // returning a reference to the integer value boxed as an object.
        writeStrIL.Emit(OpCodes.Box, int.class.ToType());

        // Atop the stack, you'll find our string inStr, followed by a 
        // reference to the boxed value of 'x'. Now, you need to likewise
        // box field 'y'.
        writeStrIL.Emit(OpCodes.Ldarg_0);
        writeStrIL.Emit(OpCodes.Ldfld, yField);
        writeStrIL.Emit(OpCodes.Box, int.class.ToType());

        // Now, you have all of the arguments for your call to
        // Console.WriteLine(string, object, object) atop the stack:
        // the string InStr, a reference to the boxed value of 'x', and
        // a reference to the boxed value of 'y'.
        // Call Console.WriteLine(string, object, object) with EmitCall.
        writeStrIL.EmitCall(OpCodes.Call, writeLineMI, null);

        // Lastly, EmitWriteLine can also output the value of a field
        // using the overload EmitWriteLine(FieldInfo).
        writeStrIL.EmitWriteLine("The value of 'x' is:");
        writeStrIL.EmitWriteLine(xField);
        writeStrIL.EmitWriteLine("The value of 'y' is:");
        writeStrIL.EmitWriteLine(yField);

        // Since we return no value (void), the the ret opcode will not
        // return the top stack value.
        writeStrIL.Emit(OpCodes.Ret);
        return pointTypeBld.CreateType();
    } //CreateDynamicType
    
    public static void main(String[] args)
    {
        Object ctorParams[] = new Object[2];
        Console.Write("Enter a integer value for X: ");
        String myX = Console.ReadLine();
        Console.Write("Enter a integer value for Y: ");
        String myY = Console.ReadLine();
        Console.WriteLine("---");
        ctorParams[0] = (Int32)Integer.parseInt(myX);
        ctorParams[1] = (Int32)Integer.parseInt(myY);
        Type ptType = CreateDynamicType();
        Object ptInstance = Activator.CreateInstance(ptType, ctorParams);
        ptType.InvokeMember("WritePoint", BindingFlags.InvokeMethod, null,
            ptInstance, new Object[0]);
    } //main
} //EmitWriteLineDemo

System.Object
  System.Reflection.Emit.OpCodes

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