InstructionEncoder 結構
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
編碼通用中繼語言 (CIL) 指示。
public value class InstructionEncoder
public readonly struct InstructionEncoder
public struct InstructionEncoder
type InstructionEncoder = struct
Public Structure InstructionEncoder
- 繼承
範例
此範例展示了如何利用:InstructionEncoder
// The following code emits a method body similar to this C# code:
/*public static double CalcRectangleArea(double length, double width)
{
if (length < 0.0)
{
throw new ArgumentOutOfRangeException("length");
}
if (width < 0.0)
{
throw new ArgumentOutOfRangeException("width");
}
return length * width;
}*/
private static InstructionEncoder EmitMethodBody(MetadataBuilder metadata, AssemblyReferenceHandle corlibAssemblyRef)
{
var codeBuilder = new BlobBuilder();
var encoder = new InstructionEncoder(codeBuilder, new ControlFlowBuilder());
// Get a reference to the System.ArgumentOutOfRangeException type
TypeReferenceHandle typeRefHandle = metadata.AddTypeReference(
corlibAssemblyRef,
metadata.GetOrAddString("System"),
metadata.GetOrAddString("ArgumentOutOfRangeException"));
// Signature: .ctor(string)
var ctorSignature = new BlobBuilder();
new BlobEncoder(ctorSignature).
MethodSignature(isInstanceMethod: true).
Parameters(1, returnType => returnType.Void(), parameters => parameters.AddParameter().Type().String());
BlobHandle ctorBlobIndex = metadata.GetOrAddBlob(ctorSignature);
// Get a reference to the System.ArgumentOutOfRangeException constructor
MemberReferenceHandle ctorMemberRef = metadata.AddMemberReference(
typeRefHandle,
metadata.GetOrAddString(".ctor"),
ctorBlobIndex);
LabelHandle label1 = encoder.DefineLabel();
LabelHandle label2 = encoder.DefineLabel();
// ldarg.0
encoder.OpCode(ILOpCode.Ldarg_0);
// ldc.r8 0
encoder.LoadConstantR8(0);
// bge.un.s LABEL1
encoder.Branch(ILOpCode.Bge_un_s, label1);
// ldstr "length"
encoder.LoadString(metadata.GetOrAddUserString("length"));
// newobj instance void [System.Runtime]System.ArgumentOutOfRangeException::.ctor(string)
encoder.OpCode(ILOpCode.Newobj);
encoder.Token(ctorMemberRef);
// throw
encoder.OpCode(ILOpCode.Throw);
// LABEL1: ldarg.1
encoder.MarkLabel(label1);
encoder.OpCode(ILOpCode.Ldarg_1);
// ldc.r8 0
encoder.LoadConstantR8(0);
// bge.un.s LABEL2
encoder.Branch(ILOpCode.Bge_un_s, label2);
// ldstr "width"
encoder.LoadString(metadata.GetOrAddUserString("width"));
// newobj instance void [System.Runtime]System.ArgumentOutOfRangeException::.ctor(string)
encoder.OpCode(ILOpCode.Newobj);
encoder.Token(ctorMemberRef);
// throw
encoder.OpCode(ILOpCode.Throw);
// LABEL2: ldarg.0
encoder.MarkLabel(label2);
encoder.OpCode(ILOpCode.Ldarg_0);
// ldarg.1
encoder.OpCode(ILOpCode.Ldarg_1);
// mul
encoder.OpCode(ILOpCode.Mul);
// ret
encoder.OpCode(ILOpCode.Ret);
return encoder;
}
備註
此 InstructionEncoder 類別用於發出構成方法本體的 CIL 指令。 關於方法的發射完整範例,請參閱 MetadataBuilder 類別文件。
建構函式
| 名稱 | Description |
|---|---|
| InstructionEncoder(BlobBuilder, ControlFlowBuilder) |
建立一個由程式碼與控制流程建構器支持的編碼器。 |
屬性
| 名稱 | Description |
|---|---|
| CodeBuilder |
底層建構器,編碼指令被寫入。 |
| ControlFlowBuilder |
建構者追蹤標籤、分支與例外處理程式。 |
| Offset |
下一條編碼指令的偏移量。 |
方法
| 名稱 | Description |
|---|---|
| Branch(ILOpCode, LabelHandle) |
編碼分支指令。 |
| Call(EntityHandle) |
編碼 |
| Call(MemberReferenceHandle) |
編碼 |
| Call(MethodDefinitionHandle) |
編碼 |
| Call(MethodSpecificationHandle) |
編碼 |
| CallIndirect(StandaloneSignatureHandle) |
編碼 |
| DefineLabel() |
定義一個標籤,日後可用來標記並指向指令流中的某個位置。 |
| LoadArgument(Int32) |
編碼參數載入指令。 |
| LoadArgumentAddress(Int32) |
編碼參數位址載入指令。 |
| LoadConstantI4(Int32) |
編碼 Int32 恆定負載指令。 |
| LoadConstantI8(Int64) |
編碼 Int64 恆定負載指令。 |
| LoadConstantR4(Single) |
編碼 Single 恆定負載指令。 |
| LoadConstantR8(Double) |
編碼 Double 恆定負載指令。 |
| LoadLocal(Int32) |
編碼區域變數載入指令。 |
| LoadLocalAddress(Int32) |
編碼區域變數位址載入指令。 |
| LoadString(UserStringHandle) |
編碼 |
| MarkLabel(LabelHandle) |
關聯指定的標籤與當前的 IL 偏移量。 |
| OpCode(ILOpCode) |
編碼指定的操作碼。 |
| StoreArgument(Int32) |
編碼參數儲存指令。 |
| StoreLocal(Int32) |
編碼區域變數儲存指令。 |
| Switch(Int32) |
開始編碼交換指令。 |
| Token(EntityHandle) |
編碼一個代幣。 |
| Token(Int32) |
編碼一個代幣。 |