Cer 枚举


在受约束的执行区域内调用时指定方法的行为。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 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 0

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

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.