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
继承
属性

字段

名称 说明
None 0

方法、类型或程序集没有 CER 的概念。 它不利用 CER 保证。

MayFail 1

面对异常情况,该方法可能会失败。 在这种情况下,该方法将报告回调用方法是成功还是失败。 该方法必须围绕方法正文具有 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

适用于

另请参阅