Emit Method (OpCode, Label[])

ILGenerator.Emit Method (OpCode, Label())

[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]

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

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

'Declaration
Public Overridable Sub Emit ( _
	opcode As OpCode, _
	labels As Label() _
)

Parameters

opcode
Type: System.Reflection.Emit.OpCode
The MSIL instruction to be emitted onto the stream.
labels
Type: System.Reflection.Emit.Label ()
The array of label objects that forms the jump table.

ExceptionCondition
ArgumentNullException

labels is Nothing.

This method overload emits a switch table.

The instruction values are defined in the OpCodes enumeration.

Labels are created by using the DefineLabel method, and their location within the stream is fixed by using the MarkLabel method. If a single-byte instruction is used, each label can represent a jump of at most 127 bytes along the stream. opcode must represent a branch instruction. Because branches are relative instructions, during the fixup process each label will be replaced with the correct offset to branch to.

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.


Imports System.Reflection
Imports System.Reflection.Emit

Class Example

   Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)

      Dim myDomain As AppDomain = AppDomain.CurrentDomain
      Dim myAsmName As New AssemblyName("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()

      ' Initialize the 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 method argument, 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)

      Dim myType As Type = myTypeBuilder.CreateType()

      Dim testValues() As Integer = { 3, 500, 0 }
      Dim obj As Object = Activator.CreateInstance(myType)
      For Each testValue As Integer In testValues

         Dim result As Object = _
            myType.InvokeMember("SwitchMe", _
                                BindingFlags.InvokeMethod, _
                                Type.DefaultBinder, _
                                obj, _
                                New Object() { testValue })

         outputBlock.Text &= String.Format("Yes, there {0} today!", result) & vbLf
      Next

   End Sub 
End Class 

' This example produces the following output:
'
'Yes, there are three bananas today!
'Yes, there are many bananas today!
'Yes, there are no bananas today!


Windows Phone OS

Supported in: 8.1, 8.0, 7.1

Windows Phone

Show:
© 2017 Microsoft