Code contracts-precondition failed

Source: Internet
Author: User
Tags visual studio 2010


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
  1. Code contracts
  2. Code contracts settings in Visual Studio 2010

 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.