Code contracts is Microsoft's implementation of programming by contract. Both. NET Framework 4.0 and 4.5 provide corresponding class library support. The contract class is defined in the system. Diagnostics. Contracts namespace of mscorelib. dll. It contains a set of static methods to define various contract, such as contract. requires and contract. Ensures. However, the Code for defining contract is not enough. You can also enable runtime or static in your project.
Contract check. Otherwise, you will see the following error message at runtime:
The corresponding Execution Code is as follows:
using System;using System.Diagnostics.Contracts;namespace ConsoleApplication1{ class Program { static void Main(string[] args) { Divide(12, 1); } static int Divide(int number, int divisor) { Contract.Requires<InvalidCastException>(divisor != 0); return number / divisor; } }}
After reading this error many times, I feel that it is quite difficult to understand the cause and how to fix it. After multiple experiments and searching for information on the Internet, you must select 'Enable runtime contract Checking' to define contracts_full. However, using contract. Requires for calling, the following code will not make the above error:
class Program { static void Main(string[] args) { Divide(12, 1); } static int Divide(int number, int divisor) { Contract.Requires(divisor != 0); return number / divisor; } }
It seems that the key problem is that the definition of contract. Requires () and contract. Requires <> () methods is different. Through. Net reflector, You can further see how the two are defined. The [conditionalattribute ("contracts_full")] attribute is defined on requires (), so that the compiler can determine whether requires () is available based on whether the contracts_full identifier is defined; while requires <> () this attribute is not available, so it is still available even if contracts_full is not defined, so the above error will occur.
[ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail), Conditional("CONTRACTS_FULL"), __DynamicallyInvokable, TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")] public static void Requires(bool condition) { AssertMustUseRewriter(ContractFailureKind.Precondition, "Requires"); }
[ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail), __DynamicallyInvokable, TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")] public static void Requires<TException>(bool condition) where TException: Exception { AssertMustUseRewriter(ContractFailureKind.Precondition, "Requires<TException>"); }
This is also the question of what in the requires <> () document focuses on-"you must turn on run-time
Checking to use therequires method. If run-time checking is turned off, the process will be terminated. To obtain the tools for runtime checking,
Seecode contracts on the msdn devlabs web site ."
Reference
- Code contracts
- Code contracts settings in Visual Studio 2010