Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

ILGenerator.Emit Method

Puts an instruction onto the Microsoft Intermediate Language (MSIL) stream for the just-in-time (JIT) compiler.

Overload List

Puts the specified instruction onto the stream of instructions.

[Visual Basic] Overloads Public Overridable Sub Emit(OpCode)
[C#] public virtual void Emit(OpCode);
[C++] public: virtual void Emit(OpCode);
[JScript] public function Emit(OpCode);

Puts the specified instruction and character argument onto the Microsoft intermediate language (MSIL) stream of instructions.

[Visual Basic] Overloads Public Overridable Sub Emit(OpCode, Byte)
[C#] public virtual void Emit(OpCode, byte);
[C++] public: virtual void Emit(OpCode, unsigned char);
[JScript] public function Emit(OpCode, Byte);

Puts the specified instruction and metadata token for the specified constructor onto the Microsoft intermediate language (MSIL) stream of instructions.

[Visual Basic] Overloads Public Overridable Sub Emit(OpCode, ConstructorInfo)
[C#] public virtual void Emit(OpCode, ConstructorInfo);
[C++] public: virtual void Emit(OpCode, ConstructorInfo*);
[JScript] public function Emit(OpCode, ConstructorInfo);

Puts the specified instruction and numerical argument onto the Microsoft intermediate language (MSIL) stream of instructions.

[Visual Basic] Overloads Public Overridable Sub Emit(OpCode, Double)
[C#] public virtual void Emit(OpCode, double);
[C++] public: virtual void Emit(OpCode, double);
[JScript] public function Emit(OpCode, double);

Puts the specified instruction and metadata token for the specified field onto the Microsoft intermediate language (MSIL) stream of instructions.

[Visual Basic] Overloads Public Overridable Sub Emit(OpCode, FieldInfo)
[C#] public virtual void Emit(OpCode, FieldInfo);
[C++] public: virtual void Emit(OpCode, FieldInfo*);
[JScript] public function Emit(OpCode, FieldInfo);

Puts the specified instruction and numerical argument onto the Microsoft intermediate language (MSIL) stream of instructions.

[Visual Basic] Overloads Public Overridable Sub Emit(OpCode, Short)
[C#] public virtual void Emit(OpCode, short);
[C++] public: virtual void Emit(OpCode, short);
[JScript] public function Emit(OpCode, Int16);

Puts the specified instruction and numerical argument onto the Microsoft intermediate language (MSIL) stream of instructions.

[Visual Basic] Overloads Public Overridable Sub Emit(OpCode, Integer)
[C#] public virtual void Emit(OpCode, int);
[C++] public: virtual void Emit(OpCode, int);
[JScript] public function Emit(OpCode, int);

Puts the specified instruction and numerical argument onto the Microsoft intermediate language (MSIL) stream of instructions.

[Visual Basic] Overloads Public Overridable Sub Emit(OpCode, Long)
[C#] public virtual void Emit(OpCode, long);
[C++] public: virtual void Emit(OpCode, __int64);
[JScript] public function Emit(OpCode, long);

Puts the specified instruction onto the Microsoft intermediate language (MSIL) stream and leaves space to include a label when fixes are done.

[Visual Basic] Overloads Public Overridable Sub Emit(OpCode, Label)
[C#] public virtual void Emit(OpCode, Label);
[C++] public: virtual void Emit(OpCode, Label);
[JScript] public function Emit(OpCode, Label);

Puts the specified instruction onto the Microsoft intermediate language (MSIL) stream and leaves space to include a label when fixes are done.

[Visual Basic] Overloads Public Overridable Sub Emit(OpCode, Label())
[C#] public virtual void Emit(OpCode, Label[]);
[C++] public: virtual void Emit(OpCode, Label[]);
[JScript] public function Emit(OpCode, Label[]);

Puts the specified instruction onto the Microsoft intermediate language (MSIL) stream followed by the index of the given local variable.

[Visual Basic] Overloads Public Overridable Sub Emit(OpCode, LocalBuilder)
[C#] public virtual void Emit(OpCode, LocalBuilder);
[C++] public: virtual void Emit(OpCode, LocalBuilder*);
[JScript] public function Emit(OpCode, LocalBuilder);

Puts the specified instruction onto the Microsoft intermediate language (MSIL) stream followed by the metadata token for the given method.

[Visual Basic] Overloads Public Overridable Sub Emit(OpCode, MethodInfo)
[C#] public virtual void Emit(OpCode, MethodInfo);
[C++] public: virtual void Emit(OpCode, MethodInfo*);
[JScript] public function Emit(OpCode, MethodInfo);

Puts the specified instruction and character argument onto the Microsoft intermediate language (MSIL) stream of instructions. This method is not CLS-compliant.

[Visual Basic] Overloads Public Sub Emit(OpCode, SByte)
[C#] public void Emit(OpCode, sbyte);
[C++] public: void Emit(OpCode, char);
[JScript] public function Emit(OpCode, SByte);

Puts the specified instruction and a signature token onto the Microsoft intermediate language (MSIL) stream of instructions.

[Visual Basic] Overloads Public Overridable Sub Emit(OpCode, SignatureHelper)
[C#] public virtual void Emit(OpCode, SignatureHelper);
[C++] public: virtual void Emit(OpCode, SignatureHelper*);
[JScript] public function Emit(OpCode, SignatureHelper);

Puts the specified instruction and numerical argument onto the Microsoft intermediate language (MSIL) stream of instructions.

[Visual Basic] Overloads Public Overridable Sub Emit(OpCode, Single)
[C#] public virtual void Emit(OpCode, float);
[C++] public: virtual void Emit(OpCode, float);
[JScript] public function Emit(OpCode, float);

Puts the specified instruction onto the Microsoft intermediate language (MSIL) stream followed by the metadata token for the given string.

[Visual Basic] Overloads Public Overridable Sub Emit(OpCode, String)
[C#] public virtual void Emit(OpCode, string);
[C++] public: virtual void Emit(OpCode, String*);
[JScript] public function Emit(OpCode, String);

Puts the specified instruction onto the Microsoft intermediate language (MSIL) stream followed by the metadata token for the given type.

[Visual Basic] Overloads Public Overridable Sub Emit(OpCode, Type)
[C#] public virtual void Emit(OpCode, Type);
[C++] public: virtual void Emit(OpCode, Type*);
[JScript] public function Emit(OpCode, Type);

Example

[Visual Basic, C#, C++] The code sample below illustrates the creation of a dynamic method with a jump table. The jump table is built using an array of Label.

[Visual Basic, C#, C++] Note   This example shows how to use one of the overloaded versions of Emit. For other examples that might be available, see the individual overload topics.
[Visual Basic] 

Imports System
Imports System.Threading
Imports System.Reflection
Imports System.Reflection.Emit

 _

Class DynamicJumpTableDemo
   
   Public Shared Function BuildMyType() As Type

      Dim myDomain As AppDomain = Thread.GetDomain()
      Dim myAsmName As New AssemblyName()
      myAsmName.Name = "MyDynamicAssembly"
      
      Dim myAsmBuilder As AssemblyBuilder = myDomain.DefineDynamicAssembly(myAsmName, _
                            AssemblyBuilderAccess.Run)
      Dim myModBuilder As ModuleBuilder = myAsmBuilder.DefineDynamicModule("MyJumpTableDemo")
      
      Dim myTypeBuilder As TypeBuilder = myModBuilder.DefineType("JumpTableDemo", _
                                 TypeAttributes.Public)
      Dim myMthdBuilder As MethodBuilder = myTypeBuilder.DefineMethod("SwitchMe", _
                        MethodAttributes.Public Or MethodAttributes.Static, _
                        GetType(String), New Type() {GetType(Integer)})
      
      Dim myIL As ILGenerator = myMthdBuilder.GetILGenerator()
      
      Dim defaultCase As Label = myIL.DefineLabel()
      Dim endOfMethod As Label = myIL.DefineLabel()
      
      ' We are initializing our jump table. Note that the labels
      ' will be placed later using the MarkLabel method. 

      Dim jumpTable() As Label = {myIL.DefineLabel(), _
                  myIL.DefineLabel(), _
                  myIL.DefineLabel(), _
                  myIL.DefineLabel(), _
                  myIL.DefineLabel()}
      
      ' arg0, the number we passed, is pushed onto the stack.
      ' In this case, due to the design of the code sample,
      ' the value pushed onto the stack happens to match the
      ' index of the label (in IL terms, the index of the offset
      ' in the jump table). If this is not the case, such as
      ' when switching based on non-integer values, rules for the correspondence
      ' between the possible case values and each index of the offsets
      ' must be established outside of the ILGenerator.Emit calls,
      ' much as a compiler would.

      myIL.Emit(OpCodes.Ldarg_0)
      myIL.Emit(OpCodes.Switch, jumpTable)
      
      ' Branch on default case
      myIL.Emit(OpCodes.Br_S, defaultCase)
      
      ' Case arg0 = 0
      myIL.MarkLabel(jumpTable(0))
      myIL.Emit(OpCodes.Ldstr, "are no bananas")
      myIL.Emit(OpCodes.Br_S, endOfMethod)
      
      ' Case arg0 = 1
      myIL.MarkLabel(jumpTable(1))
      myIL.Emit(OpCodes.Ldstr, "is one banana")
      myIL.Emit(OpCodes.Br_S, endOfMethod)
      
      ' Case arg0 = 2
      myIL.MarkLabel(jumpTable(2))
      myIL.Emit(OpCodes.Ldstr, "are two bananas")
      myIL.Emit(OpCodes.Br_S, endOfMethod)
      
      ' Case arg0 = 3
      myIL.MarkLabel(jumpTable(3))
      myIL.Emit(OpCodes.Ldstr, "are three bananas")
      myIL.Emit(OpCodes.Br_S, endOfMethod)
      
      ' Case arg0 = 4
      myIL.MarkLabel(jumpTable(4))
      myIL.Emit(OpCodes.Ldstr, "are four bananas")
      myIL.Emit(OpCodes.Br_S, endOfMethod)
      
      ' Default case
      myIL.MarkLabel(defaultCase)
      myIL.Emit(OpCodes.Ldstr, "are many bananas")
      
      myIL.MarkLabel(endOfMethod)
      myIL.Emit(OpCodes.Ret)
      
      Return myTypeBuilder.CreateType()

   End Function 'BuildMyType
    
   
   Public Shared Sub Main()

      Dim myType As Type = BuildMyType()
      
      Console.Write("Enter an integer between 0 and 5: ")
      Dim theValue As Integer = Convert.ToInt32(Console.ReadLine())
      
      Console.WriteLine("---")
      Dim myInstance As [Object] = Activator.CreateInstance(myType, New Object() {})
      Console.WriteLine("Yes, there {0} today!", myType.InvokeMember("SwitchMe", _
                         BindingFlags.InvokeMethod, Nothing, _
                             myInstance, New Object() {theValue}))

   End Sub 'Main

End Class 'DynamicJumpTableDemo


[C#] 

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

class DynamicJumpTableDemo

{

   public static Type BuildMyType()
   {
    AppDomain myDomain = Thread.GetDomain();
    AssemblyName myAsmName = new AssemblyName();
    myAsmName.Name = "MyDynamicAssembly";

    AssemblyBuilder myAsmBuilder = myDomain.DefineDynamicAssembly(
                        myAsmName,
                        AssemblyBuilderAccess.Run);
    ModuleBuilder myModBuilder = myAsmBuilder.DefineDynamicModule(
                        "MyJumpTableDemo");

    TypeBuilder myTypeBuilder = myModBuilder.DefineType("JumpTableDemo",
                            TypeAttributes.Public);
    MethodBuilder myMthdBuilder = myTypeBuilder.DefineMethod("SwitchMe", 
                             MethodAttributes.Public |
                             MethodAttributes.Static,
                                             typeof(string), 
                                             new Type[] {typeof(int)});

    ILGenerator myIL = myMthdBuilder.GetILGenerator();

    Label defaultCase = myIL.DefineLabel();    
    Label endOfMethod = myIL.DefineLabel();    

    // We are initializing our jump table. Note that the labels
    // will be placed later using the MarkLabel method. 

    Label[] jumpTable = new Label[] { myIL.DefineLabel(),
                      myIL.DefineLabel(),
                      myIL.DefineLabel(),
                      myIL.DefineLabel(),
                      myIL.DefineLabel() };

    // arg0, the number we passed, is pushed onto the stack.
    // In this case, due to the design of the code sample,
    // the value pushed onto the stack happens to match the
    // index of the label (in IL terms, the index of the offset
    // in the jump table). If this is not the case, such as
    // when switching based on non-integer values, rules for the correspondence
    // between the possible case values and each index of the offsets
    // must be established outside of the ILGenerator.Emit calls,
    // much as a compiler would.

    myIL.Emit(OpCodes.Ldarg_0);
    myIL.Emit(OpCodes.Switch, jumpTable);
    
    // Branch on default case
    myIL.Emit(OpCodes.Br_S, defaultCase);

    // Case arg0 = 0
    myIL.MarkLabel(jumpTable[0]); 
    myIL.Emit(OpCodes.Ldstr, "are no bananas");
    myIL.Emit(OpCodes.Br_S, endOfMethod);

    // Case arg0 = 1
    myIL.MarkLabel(jumpTable[1]); 
    myIL.Emit(OpCodes.Ldstr, "is one banana");
    myIL.Emit(OpCodes.Br_S, endOfMethod);

    // Case arg0 = 2
    myIL.MarkLabel(jumpTable[2]); 
    myIL.Emit(OpCodes.Ldstr, "are two bananas");
    myIL.Emit(OpCodes.Br_S, endOfMethod);

    // Case arg0 = 3
    myIL.MarkLabel(jumpTable[3]); 
    myIL.Emit(OpCodes.Ldstr, "are three bananas");
    myIL.Emit(OpCodes.Br_S, endOfMethod);

    // Case arg0 = 4
    myIL.MarkLabel(jumpTable[4]); 
    myIL.Emit(OpCodes.Ldstr, "are four bananas");
    myIL.Emit(OpCodes.Br_S, endOfMethod);

    // Default case
    myIL.MarkLabel(defaultCase);
    myIL.Emit(OpCodes.Ldstr, "are many bananas");

    myIL.MarkLabel(endOfMethod);
    myIL.Emit(OpCodes.Ret);
    
    return myTypeBuilder.CreateType();

   }

   public static void Main()
   {
    Type myType = BuildMyType();
    
    Console.Write("Enter an integer between 0 and 5: ");
    int theValue = Convert.ToInt32(Console.ReadLine());

    Console.WriteLine("---");
    Object myInstance = Activator.CreateInstance(myType, new object[0]);    
    Console.WriteLine("Yes, there {0} today!", myType.InvokeMember("SwitchMe",
                                 BindingFlags.InvokeMethod,
                                 null,
                                 myInstance,
                                 new object[] {theValue}));  
              
   }

}


[C++] 

#using <mscorlib.dll>

using namespace System;
using namespace System::Threading;
using namespace System::Reflection;
using namespace System::Reflection::Emit;

Type* BuildMyType() {
   AppDomain*  myDomain = Thread::GetDomain();
   AssemblyName* myAsmName = new AssemblyName();
   myAsmName->Name = S"MyDynamicAssembly";

   AssemblyBuilder*  myAsmBuilder = myDomain->DefineDynamicAssembly(myAsmName,
      AssemblyBuilderAccess::Run);
   ModuleBuilder*  myModBuilder = myAsmBuilder->DefineDynamicModule(S"MyJumpTableDemo");

   TypeBuilder*  myTypeBuilder = myModBuilder->DefineType(S"JumpTableDemo",
      TypeAttributes::Public);

   Type* temp0 [] = {__typeof(int)};
   MethodBuilder*  myMthdBuilder = myTypeBuilder->DefineMethod(
      S"SwitchMe",
      static_cast<MethodAttributes>(MethodAttributes::Public | MethodAttributes::Static),
      __typeof(String),
      temp0);

   ILGenerator*  myIL = myMthdBuilder->GetILGenerator();

   Label defaultCase = myIL->DefineLabel();
   Label endOfMethod = myIL->DefineLabel();

   // We are initializing our jump table. Note that the labels
   // will be placed later using the MarkLabel method.

   Label jumpTable [] = new Label[5];
   jumpTable[0] = myIL->DefineLabel();
   jumpTable[1] = myIL->DefineLabel();
   jumpTable[2] = myIL->DefineLabel();
   jumpTable[3] = myIL->DefineLabel();
   jumpTable[4] = myIL->DefineLabel();

   // arg0, the number we passed, is pushed onto the stack.
   // In this case, due to the design of the code sample,
   // the value pushed onto the stack happens to match the
   // index of the label (in IL terms, the index of the offset
   // in the jump table). If this is not the case, such as
   // when switching based on non-integer values, rules for the correspondence
   // between the possible case values and each index of the offsets
   // must be established outside of the ILGenerator::Emit calls,
   // much as a compiler would.

   myIL->Emit(OpCodes::Ldarg_0);
   myIL->Emit(OpCodes::Switch, jumpTable);

   // Branch on default case
   myIL->Emit(OpCodes::Br_S, defaultCase);

   // Case arg0 = 0
   myIL->MarkLabel(jumpTable[0]);
   myIL->Emit(OpCodes::Ldstr, S"are no bananas");
   myIL->Emit(OpCodes::Br_S, endOfMethod);

   // Case arg0 = 1
   myIL->MarkLabel(jumpTable[1]);
   myIL->Emit(OpCodes::Ldstr, S"is one banana");
   myIL->Emit(OpCodes::Br_S, endOfMethod);

   // Case arg0 = 2
   myIL->MarkLabel(jumpTable[2]);
   myIL->Emit(OpCodes::Ldstr, S"are two bananas");
   myIL->Emit(OpCodes::Br_S, endOfMethod);

   // Case arg0 = 3
   myIL->MarkLabel(jumpTable[3]);
   myIL->Emit(OpCodes::Ldstr, S"are three bananas");
   myIL->Emit(OpCodes::Br_S, endOfMethod);

   // Case arg0 = 4
   myIL->MarkLabel(jumpTable[4]);
   myIL->Emit(OpCodes::Ldstr, S"are four bananas");
   myIL->Emit(OpCodes::Br_S, endOfMethod);

   // Default case
   myIL->MarkLabel(defaultCase);
   myIL->Emit(OpCodes::Ldstr, S"are many bananas");

   myIL->MarkLabel(endOfMethod);
   myIL->Emit(OpCodes::Ret);

   return myTypeBuilder->CreateType();
}

int main() {
   Type*  myType = BuildMyType();

   Console::Write(S"Enter an integer between 0 and 5: ");
   int theValue = Convert::ToInt32(Console::ReadLine());

   Console::WriteLine(S"---");
   Object*  myInstance = Activator::CreateInstance(myType, new Object*[0]);

   Object* temp1 [] = {__box(theValue)};
   Console::WriteLine(S"Yes, there {0} today!", myType->InvokeMember(S"SwitchMe",
      BindingFlags::InvokeMethod,
      0,
      myInstance,
      temp1));
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

See Also

ILGenerator Class | ILGenerator Members | System.Reflection.Emit Namespace

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2015 Microsoft