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 區域中呼叫的也如此。 如果完成其預定要做的,則方法會成功。 例如,以標記CountReliabilityContractAttribute(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

適用於

另請參閱