# 限制的執行區域Constrained Execution Regions

CLR 會事先準備 CER，以避免記憶體不足的情況。The CLR prepares CERs in advance to avoid out-of-memory conditions. 需要事先準備，讓 CLR 不會在 Just-In-Time 編譯或類型載入期間導致記憶體不足。Advance preparation is required so the CLR does not cause an out of memory condition during just-in-time compilation or type loading.

## 條件約束Constraints

• 明確配置。Explicit allocation.

• Boxing。Boxing.

• 取得鎖定。Acquiring a lock.

• 幾乎會呼叫未備妥的方法。Calling unprepared methods virtually.

• 呼叫具有弱式或不存在可靠性合約的方法。Calling methods with a weak or nonexistent reliability contract.

## 可靠性合約Reliability Contracts

ReliabilityContractAttribute 是一個自訂屬性，記載所指定方法的可靠性保證和損毀狀態。The ReliabilityContractAttribute is a custom attribute that documents the reliability guarantees and the corruption state of a given method.

### 可靠性保證Reliability Guarantees

• 在例外狀況下，此方法可能會失敗。Under exceptional conditions, the method might fail. 在此情況下，此方法會向呼叫端方法回報成功還是失敗。In this case, the method reports back to the calling method whether it succeeded or failed. 此方法必須包含在 CER 中，確保它可以報告傳回值。The method must be contained in a CER to ensure that it can report the return value.

• None.None. 方法、類型或組件沒有 CER 的概念，而且很可能無法在 CER 內安全地呼叫，進而大幅降低狀態損毀。The method, type, or assembly has no concept of a CER and is most likely not safe to call within a CER without substantial mitigation from state corruption. 它不會利用 CER 保證。It does not take advantage of CER guarantees. 這具有如下表示：This implies the following:

1. 在例外狀況下，此方法可能會失敗。Under exceptional conditions the method might fail.

2. 此方法不一定會報告失敗。The method might or might not report that it failed.

3. 不會寫入此方法來使用 CER，這是最常見案例。The method is not written to use a CER, the most likely scenario.

4. 如果未將方法、類型或組件明確地識別為成功，則會將它隱含地識別為 NoneIf a method, type, or assembly is not explicitly identified to succeed, it is implicitly identified as None.

• 在例外狀況下，此方法一定會成功。Under exceptional conditions, the method is guaranteed to succeed. 為了達到這個層級的可靠性，您應該一律建構所呼叫方法的 CER，即使是從非 CER 區域內呼叫它也是一樣。To achieve this level of reliability 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, although success can be viewed subjectively. 例如，將 Count 標上 ReliabilityContractAttribute(Cer.Success) 表示在 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. 不過，也會將 CompareExchange 方法標記為成功，並了解成功可能表示因競爭條件而無法將值取代為新值。However, the CompareExchange method is marked as success as well, with the understanding that success may mean the value could not be replaced with a new value due to a race condition. 重點在於此方法會依記載的運作方式運作，而且不需要寫入 CER 程式碼，以預期正確行為以外的任何異常行為，但不可靠程式碼看起來都一樣。The key point is that the method behaves in the way it is documented to behave, and CER code does not need to be written to expect any unusual behavior beyond what correct but unreliable code would look like.

### 損毀層級Corruption levels

• 在例外狀況下，Common Language Runtime (CLR) 不保證目前應用程式定義域中的狀態一致性。Under exceptional conditions, the common language runtime (CLR) makes no guarantees regarding state consistency in the current application domain.

• 在例外狀況下，保證此方法將狀態損毀限制為目前執行個體。Under exceptional conditions, the method is guaranteed to limit state corruption to the current instance.

• MayCorruptProcess：在例外狀況下，CLR 不保證相關狀態一致性；亦即，此狀況可能會損毀處理序。MayCorruptProcess, Under exceptional conditions, the CLR makes no guarantees regarding state consistency; that is, the condition might corrupt the process.

• 在例外狀況下，此方法一定不會損毀狀態。Under exceptional conditions, the method is guaranteed not to corrupt state.

## CER 中不允許的程式碼Code Not Permitted in CERs

CER 中不允許下列作業：The following operations are not permitted in CERs:

• 明確配置。Explicit allocations.

• 取得鎖定。Acquiring a lock.

• Boxing。Boxing.

• 多維陣列存取。Multidimensional array access.

• 透過反映的方法呼叫。Method calls through reflection.

• Enter or Lock.

• 安全性檢查。Security checks. 請不要執行要求，而是連結要求。Do not perform demands, only link demands.

• COM 物件和 Proxy 的 IsinstCastclassIsinst and Castclass for COM objects and proxies

• 取得或設定 Transparent Proxy 上的欄位。Getting or setting fields on a transparent proxy.

• 序列化：Serialization.

• 函式指標和委派。Function pointers and delegates.