Udostępnij za pośrednictwem


OpCodes.TakesSingleByteArgument Method

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Returns true or false if the supplied opcode takes a single byte argument.

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

Syntax

'Declaration
Public Shared Function TakesSingleByteArgument ( _
    inst As OpCode _
) As Boolean
public static bool TakesSingleByteArgument(
    OpCode inst
)

Parameters

Return Value

Type: System.Boolean
True or false.

Remarks

This method can be used to find which MSIL opcodes are "short form", for use in optimized code.

TakesSingleByteArgument returns true if the OpCode instance takes a single byte argument in the following cases:

  • The opcode performs a branch instruction to a byte-sized address (for example, Br_S and Bgt_S).

  • The opcode pushes a byte value onto the stack (for example, Ldc_I4_S).

  • The opcode references a variable or argument via the byte-sized "short form" (for example, Ldloc_S and Stloc_S).

Otherwise, it returns false.

Examples

The example below demonstrates the use of TakesSingleByteArgument by reflecting on the OpCodes class and testing to see whether each OpCode field takes a single-byte argument.

Imports System.Reflection
Imports System.Reflection.Emit

Class Example

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

      outputBlock.Text &= _
         "List the OpCodes that take single-byte arguments:" & vbLf

      ' Blank OpCode object to use when calling FieldInfo.GetValue().
      Dim blankOpCode As New OpCode()

      ' Reflect on all the fields in System.Reflection.Emit.OpCodes.
      ' For each field, create an instance of the OpCode it represents,
      ' then test the OpCode.
      For Each fi As FieldInfo In GetType(OpCodes).GetFields() 

         ' Get the OpCode, which is the value of the field. GetValue
         ' returns Object, so the result must be cast to OpCode.
         Dim oc As OpCode = CType(fi.GetValue(blankOpCode), OpCode)

         If OpCodes.TakesSingleByteArgument(oc) Then
            outputBlock.Text &= oc.Name & " - " & fi.Name & vbLf
         End If
      Next 

   End Sub 
End Class 

' This example produces code similar to the following:
'
'List the OpCodes that take single-byte arguments:
'ldarg.s - Ldarg_S
'ldarga.s - Ldarga_S
'starg.s - Starg_S
'ldloc.s - Ldloc_S
'ldloca.s - Ldloca_S
'stloc.s - Stloc_S
'ldc.i4.s - Ldc_I4_S
'br.s - Br_S
'brfalse.s - Brfalse_S
'brtrue.s - Brtrue_S
'beq.s - Beq_S
'bge.s - Bge_S
'bgt.s - Bgt_S
'ble.s - Ble_S
'blt.s - Blt_S
'bne.un.s - Bne_Un_S
'bge.un.s - Bge_Un_S
'bgt.un.s - Bgt_Un_S
'ble.un.s - Ble_Un_S
'blt.un.s - Blt_Un_S
'leave.s - Leave_S
'unalignedl - Unaligned
using System.Reflection;
using System.Reflection.Emit;

class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      outputBlock.Text += 
         "List the OpCodes that take single-byte arguments:\n";

      // Blank OpCode object to use when calling FieldInfo.GetValue().
      OpCode blankOpCode = new OpCode();

      // Reflect on all the fields in System.Reflection.Emit.OpCodes.
      // For each field, create an instance of the OpCode it represents,
      // then test the OpCode.
      foreach (FieldInfo fi in typeof(OpCodes).GetFields())
      {
         // Get the OpCode, which is the value of the field. GetValue
         // returns Object, so the result must be cast to OpCode.
         OpCode oc = (OpCode) fi.GetValue(blankOpCode);

         if (OpCodes.TakesSingleByteArgument(oc))
         {
            outputBlock.Text += oc.Name + " - " + fi.Name + "\n";
         }
      }
   }
}

/* This example produces code similar to the following:

List the OpCodes that take single-byte arguments:
ldarg.s - Ldarg_S
ldarga.s - Ldarga_S
starg.s - Starg_S
ldloc.s - Ldloc_S
ldloca.s - Ldloca_S
stloc.s - Stloc_S
ldc.i4.s - Ldc_I4_S
br.s - Br_S
brfalse.s - Brfalse_S
brtrue.s - Brtrue_S
beq.s - Beq_S
bge.s - Bge_S
bgt.s - Bgt_S
ble.s - Ble_S
blt.s - Blt_S
bne.un.s - Bne_Un_S
bge.un.s - Bge_Un_S
bgt.un.s - Bgt_Un_S
ble.un.s - Ble_Un_S
blt.un.s - Blt_Un_S
leave.s - Leave_S
unalignedl - Unaligned
 */

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.