TakesSingleByteArgument Method
Collapse the table of content
Expand the table of content

OpCodes.TakesSingleByteArgument Method

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

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

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

public static bool TakesSingleByteArgument(
	OpCode inst
)

Parameters

inst
Type: System.Reflection.Emit.OpCode
An instance of an Opcode object.

Return Value

Type: System.Boolean
True or false.

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.

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.


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
 */


Windows Phone OS

Supported in: 8.1, 8.0, 7.1

Windows Phone

Show:
© 2017 Microsoft