ContractOptionAttribute 類別

定義

可讓您以組件、型別或方法的資料粒度設定合約及工具選項。

public ref class ContractOptionAttribute sealed : Attribute
[System.AttributeUsage(System.AttributeTargets.All, AllowMultiple=true, Inherited=false)]
[System.Diagnostics.Conditional("CONTRACTS_FULL")]
public sealed class ContractOptionAttribute : Attribute
[<System.AttributeUsage(System.AttributeTargets.All, AllowMultiple=true, Inherited=false)>]
[<System.Diagnostics.Conditional("CONTRACTS_FULL")>]
type ContractOptionAttribute = class
    inherit Attribute
Public NotInheritable Class ContractOptionAttribute
Inherits Attribute
繼承
ContractOptionAttribute
屬性

備註

下表顯示目前支持的選項。

類別 設定 值/效果
合約 繼承 true 開啟合約繼承; false 將它關閉。 預設為 true
執行階段 檢查 true 開啟運行時間檢查; false 將它關閉。 預設為 true

您可以使用此屬性,如下列範例所示。

若要關閉整個元件的執行時間檢查:

[assembly:ContractOption("runtime", "checking", false)]  

若要開啟特定類型的執行時間合約檢查:

[ContractOption("runtime", "checking", true)]  
class TypeWithRuntimeChecking {  

    ...  

若要關閉特定方法的執行時間檢查:

// Turn off all contract inheritance from interface IList<T>  
[ContractOption("contract", "inheritance", false)]  
class MyConcurrentList<T> : IList<T> {  
   ...  
}  

[ContractOption("runtime", "checking", false)]  
public override MyMethod(int x) {  
   // no inherited contracts checked at runtime,  
   // no invariants checked at runtime.  
   ...  
}  

       [ContractOption("runtime", "checking", false)]  
       public void MethodWithoutRuntimeChecking(...) {  
          ...  
       }  
   }  

建構函式

ContractOptionAttribute(String, String, Boolean)

使用提供的分類、設定和啟用/停用值,初始化 ContractOptionAttribute 類別的新執行個體。

ContractOptionAttribute(String, String, String)

使用提供的分類、設定和值,初始化 ContractOptionAttribute 類別的新執行個體。

屬性

Category

取得選項的分類。

Enabled

判斷是否選項已啟用。

Setting

取得選項的設定。

TypeId

在衍生類別中實作時,取得這個 Attribute 的唯一識別碼。

(繼承來源 Attribute)
Value

取得選項的值。

方法

Equals(Object)

傳回值,這個值指出此執行個體是否與指定的物件相等。

(繼承來源 Attribute)
GetHashCode()

傳回這個執行個體的雜湊碼。

(繼承來源 Attribute)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
IsDefaultAttribute()

在衍生類別中覆寫時,表示這個執行個體的值是衍生類別的預設值。

(繼承來源 Attribute)
Match(Object)

在衍生類別中覆寫時,會傳回值,表示這個執行個體是否等於指定物件。

(繼承來源 Attribute)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
ToString()

傳回代表目前物件的字串。

(繼承來源 Object)

明確介面實作

_Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr)

將一組名稱對應至一組對應的分派識別項 (Dispatch Identifier)。

(繼承來源 Attribute)
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

擷取物件的類型資訊,可以用來取得介面的類型資訊。

(繼承來源 Attribute)
_Attribute.GetTypeInfoCount(UInt32)

擷取物件提供的類型資訊介面數目 (0 或 1)。

(繼承來源 Attribute)
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

提供物件所公開的屬性和方法的存取權。

(繼承來源 Attribute)

適用於