共用方式為


OpCodes.TakesSingleByteArgument(OpCode) 方法

定義

如果提供的 Opcode 採用單一位元組引數,則傳回 True 或 False。

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

Opcode 物件的執行個體。

傳回

truefalse

備註

這個方法可用來尋找哪些 MSIL opcode 為「簡短形式」,以用於優化程序代碼。

TakesSingleByteArgument true如果OpCode實體在下列情況下採用單一位元組自變數,則會傳回 :

  • Opcode 會對位元組大小的位址執行分支指令 (,例如 Br_S ,以及 Bgt_S) 。

  • opcode 會將位元組值推送至堆疊 (例如, Ldc_I4_S) 。

  • opcode 會透過位元組大小的「簡短格式」 (參考變數或自變數, 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

適用於