Cer 列舉

定義

警告

The Constrained Execution Region (CER) feature is not supported.

指定在限制執行區域內呼叫時的方法行為。

public enum class Cer
[System.Obsolete("The Constrained Execution Region (CER) feature is not supported.", DiagnosticId="SYSLIB0004", UrlFormat="https://aka.ms/dotnet-warnings/{0}")]
public enum Cer
public enum Cer
[System.Serializable]
public enum Cer
[<System.Obsolete("The Constrained Execution Region (CER) feature is not supported.", DiagnosticId="SYSLIB0004", UrlFormat="https://aka.ms/dotnet-warnings/{0}")>]
type Cer = 
type Cer = 
[<System.Serializable>]
type Cer = 
Public Enum Cer
繼承
屬性

欄位

名稱 Description
None 0

該方法、類型或組裝本身並不包含 CER 的概念。 它不會利用 CER 保證。

MayFail 1

在特殊情況下,該方法可能會失敗。 在這種情況下,方法會回報呼叫方法是否成功。 方法必須在方法體周圍設置 CER 以確保能回報回傳值。

Success 2

面對特殊條件,該方法必然成功。 即使呼叫範圍內非 CER 區域,你也應該圍繞被呼叫的方法構建 CER。 一個方法成功時,就是達成預期效果。 例如,標記 Count 表示 ReliabilityContractAttribute(Cer.Success) 當它在 CER 下執行時,總是回傳元素數量的計 ArrayList 數,且永遠無法離開內部欄位的未定狀態。

範例

以下程式碼範例展示了在指定方法受限執行區域時, Cer 列舉的使用方式。 此程式碼範例是建構子更大範例 ReliabilityContractAttribute 的一部分。

[ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
[MethodImpl(MethodImplOptions.NoInlining)]
void StackDepth2()
{
    try
    {
        consistentLevel2 = false;
        if (depth == 2)
            Thread.Sleep(-1);
        StackDepth3();
    }
    finally
    {
        consistentLevel2 = true;
    }
}
<ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)>
<MethodImpl(MethodImplOptions.NoInlining)>
Sub StackDepth2()
   Try
      consistentLevel2 = False
      If depth = 2 Then Thread.Sleep(-1)
      StackDepth3()
   Finally
      consistentLevel2 = True
   End Try
End Sub

備註

Cer 舉指定了方法、型別或組合在受限執行區域(CER)中的行為。 使用三個可用值之一來表示該實體將成功、不了解 CER,或可能(確定性地)能夠報告成功或失敗。

CER 保證即使發生非同步異常(如中止執行緒、記憶體外溢出或堆疊溢位)時,該程式碼區域仍能不中斷執行。

然而,列 Cer.None 舉值表示該方法、型別或組裝體沒有 CER 概念。 它不會利用 CER 保證。 這具有如下表示:

  • 在特殊情況下,該方法可能會失敗。

  • 這個方法可能會報告失敗,也可能不會(因為它是非確定性的)。

  • 這個方法並非以 CER 為考量(這是最可能的情況)。

若某方法、型別或組件未明確標記為成功,則隱含標記為 Cer.None

適用於

另請參閱