System.Diagnostics.Contracts.ContractOptionAttribute Class

Enables you to set contract and tool options at assembly, type, or method granularity.

See Also: ContractOptionAttribute Members

Syntax

[System.AttributeUsage(System.AttributeTargets.All, AllowMultiple=true, Inherited=false)]
[System.Diagnostics.Conditional("CONTRACTS_FULL")]
public sealed class ContractOptionAttribute : Attribute

Remarks

The following table shows the currently supported options.

contract

inheritance

true to turn contract inheritance on; false to turn it off. The default is true.

runtime

checking

true to turn run-time checking on; false to turn it off. The default is true.

You can use this attribute as illustrated in the following examples.

To turn off run-time checking for the entire assembly:

Example

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

To turn run-time contract checking on for a specific type:

Example

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

To turn run-time checking off for a specific method:

Example

// 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(...) {
          ...
       }
   }

Requirements

Namespace: System.Diagnostics.Contracts
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 4.0.0.0