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

适用于

另请参阅