Cer Cer Cer Cer Enum


在受约束的执行区域内调用时指定方法的行为。Specifies a method's behavior when called within a constrained execution region.

public enum class Cer
public enum Cer
type Cer = 
Public Enum Cer


MayFail MayFail MayFail MayFail 1

在遇到异常情况时,此方法可能会失败。In the face of exceptional conditions, the method might fail. 在这种情况下,此方法将向调用方法报告它是否成功。In this case, the method will report back to the calling method whether it succeeded or failed. 该方法的方法体周围必须有 CER 以确保它可以报告返回值。The method must have a CER around the method body to ensure that it can report the return value.

None None None None 0

方法、类型或程序集没有 CER 的概念。The method, type, or assembly has no concept of a CER. 它不利用 CER 保证。It does not take advantage of CER guarantees.

Success Success Success Success 2

在遇到异常情况时,保证此方法获得成功。In the face of exceptional conditions, the method is guaranteed to succeed. 应始终在调用的方法周围构造 CER,即使是在非 CER 区域内调用该方法。You should always construct a CER around the method that is called, even when it is called from within a non-CER region. 如果方法完成了其任务,则该方法成功。A method is successful if it accomplishes what is intended. 例如,用 ReliabilityContractAttribute(Cer.Success) 标记 Count 意味着当它在 CER 下运行时,它始终返回 ArrayList 中的元素的数目计数,并且它永远不能将内部的字段保留为不确定状态。For example, marking Count with ReliabilityContractAttribute(Cer.Success) implies that when it is run under a CER, it always returns a count of the number of elements in the ArrayList and it can never leave the internal fields in an undetermined state.


下面的代码示例演示如何将Cer枚举指定一种方法的受约束的执行区域时。The following code example demonstrates the use of the Cer enumeration when specifying a constrained execution region for a method. 此代码示例是为提供一个更大示例的一部分ReliabilityContractAttribute构造函数。This code example is part of a larger example provided for the ReliabilityContractAttribute constructor.

[ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
void StackDepth2()
        consistentLevel2 = false;
        if (depth == 2)
        consistentLevel2 = true;
<ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)>
Sub StackDepth2()
      consistentLevel2 = False
      If depth = 2 Then Thread.Sleep(-1)
      consistentLevel2 = True
   End Try
End Sub


Cer枚举指定的方法、 类型或受约束的执行区域 (CER) 中的程序集的行为。The Cer enumeration specifies the behavior of a method, type, or assembly within a constrained execution region (CER). 使用以下任意三个可用值以指示该实体将会成功,并不知道是 CER,或可能 (肯定会) 可以报告成功或失败。Use one of the three available values to indicate that the entity will succeed, has no knowledge of a CER, or might (deterministically) be able to report success or failure.

Cer 保证将执行的代码区域不间断地即使引发异步如中止的线程的内存不足异常或堆栈溢出异常。A CER provides guarantees that the region of code will execute uninterrupted even if an asynchronous exception such as an aborted thread out-of-memory exception, or stack overflow is raised.

但是,Cer.None枚举值指示方法、 类型或程序集没有 CER 的概念。However, the Cer.None enumeration value indicates that the method, type, or assembly has no concept of a CER. 它不利用 CER 保证。It does not take advantage of CER guarantees. 这意味着:This implies the following:

  • 遇到异常情况时此方法可能会失败。In the face of exceptional conditions the method might fail.

  • 该方法可能会也可能不失败的报告 (它是不确定)。The method might or might not report that it failed (it is non-deterministic).

  • 该方法不是编写 Cer 记住 (这是最可能的方案)。The method is not written with CERs in mind (which is the most likely scenario).

如果方法、 类型或程序集未被显式标记为成功的它将隐式标记为Cer.NoneIf a method, type, or assembly is not explicitly marked to succeed, it is implicitly marked as Cer.None.