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
繼承
屬性

欄位

MayFail 1

在例外條件下,此方法可能失敗。 在這種情況下,此方法會向呼叫方法報告成功或失敗。 此方法必須在方法主體的週圍具備 CER 以確保它可以報告傳回值。

None 0

方法、型別或組件沒有 CER 概念。 它不會利用 CER 保證。

Success 2

在例外條件下,此方法會保證成功。 應永遠在呼叫的方法週圍建構 CER,即使它是從非 CER 區域中呼叫的也如此。 如果完成其預定要做的,則方法會成功。 例如,以 ReliabilityContractAttribute(Cer.Success) 標記 Count,表示在 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

適用於

另請參閱