OpCodes.TakesSingleByteArgument(OpCode) 方法

定义

如果提供的操作码采用单字节自变量则返回真或假。

public:
 static bool TakesSingleByteArgument(System::Reflection::Emit::OpCode inst);
public static bool TakesSingleByteArgument (System.Reflection.Emit.OpCode inst);
static member TakesSingleByteArgument : System.Reflection.Emit.OpCode -> bool
Public Shared Function TakesSingleByteArgument (inst As OpCode) As Boolean

参数

inst
OpCode

操作码对象的实例。

返回

truefalse

注解

此方法可用于查找哪些 MSIL 操作码是“短格式”,以便在优化代码中使用。

TakesSingleByteArgument true如果OpCode实例在以下情况下采用单个字节参数,则返回 :

  • 操作码对字节大小的地址 (执行分支指令,例如, Br_SBgt_S) 。

  • 操作码将字节值推送到堆栈 (, Ldc_I4_S 例如,) 。

  • 操作码通过字节大小的“short form” (引用变量或参数,例如, Ldloc_S) Stloc_S

否则,将返回 false

下面的示例演示了如何使用 TakesSingleByteArgument ,具体方法是对 类进行反思 OpCodes ,并测试每个 OpCode 字段是否采用单字节参数。

int main()
{
   
   // We need a blank OpCode Object for reference when calling FieldInfo::GetValue().
   OpCode blankOpCode;
   Type^ myOpCodesType = Type::GetType( "System.Reflection.Emit.OpCodes" );
   array<FieldInfo^>^listOfOpCodes = myOpCodesType->GetFields();
   Console::WriteLine( "Which OpCodes take single-Byte arguments?" );
   Console::WriteLine( "-----------------------------------------" );
   
   // Now, let's reflect on each FieldInfo and create an instance of the OpCode it represents.
   System::Collections::IEnumerator^ myEnum = listOfOpCodes->GetEnumerator();
   while ( myEnum->MoveNext() )
   {
      FieldInfo^ opCodeFI = safe_cast<FieldInfo^>(myEnum->Current);
      Object^ theOpCode = opCodeFI->GetValue( blankOpCode );
      Console::WriteLine( " {0}: {1}", opCodeFI->Name, OpCodes::TakesSingleByteArgument(  *dynamic_cast<OpCode^>(theOpCode) ) );
   }
}

public static void Main()
{
   // We need a blank OpCode object for reference when calling FieldInfo.GetValue().

   OpCode blankOpCode = new OpCode();

   Type myOpCodesType = Type.GetType("System.Reflection.Emit.OpCodes");
   FieldInfo[] listOfOpCodes = myOpCodesType.GetFields();

   Console.WriteLine("Which OpCodes take single-byte arguments?");
   Console.WriteLine("-----------------------------------------");

   // Now, let's reflect on each FieldInfo and create an instance of the OpCode it represents.

   foreach (FieldInfo opCodeFI in listOfOpCodes)
   {
    object theOpCode = opCodeFI.GetValue(blankOpCode);
    
    Console.WriteLine("{0}: {1}", opCodeFI.Name,
              OpCodes.TakesSingleByteArgument((OpCode)theOpCode).ToString());
   }
}

Public Shared Sub Main()

   ' We need a blank OpCode object for reference when calling FieldInfo.GetValue().

   Dim blankOpCode As New OpCode()
   
   Dim myOpCodesType As Type = Type.GetType("System.Reflection.Emit.OpCodes")
   Dim listOfOpCodes As FieldInfo() = myOpCodesType.GetFields()
   
   Console.WriteLine("Which OpCodes take single-byte arguments?")
   Console.WriteLine("-----------------------------------------")
   
   ' Now, let's reflect on each FieldInfo and create an instance of the OpCode it represents.
   Dim opCodeFI As FieldInfo
   For Each opCodeFI In  listOfOpCodes
      Dim theOpCode As Object = opCodeFI.GetValue(blankOpCode)
      
      Console.WriteLine("{0}: {1}", opCodeFI.Name, _
            OpCodes.TakesSingleByteArgument(CType(theOpCode, OpCode)).ToString())
   Next opCodeFI

End Sub

适用于