2 // System.Diagnostics.Contracts.Contract.cs
5 // Miguel de Icaza (miguel@gnome.org)
6 // Chris Bacon (chrisbacon76@gmail.com)
7 // Marek Safar (marek.safar@gmail.com)
9 // Copyright 2009, 2010 Novell (http://www.novell.com)
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 #if NET_4_0 || NET_2_1
34 using System.Collections.Generic;
35 using System.Diagnostics.Contracts.Internal;
36 using System.Runtime.ConstrainedExecution;
38 namespace System.Diagnostics.Contracts
40 public static class Contract
47 static event EventHandler<ContractFailedEventArgs> ContractFailed;
50 internal static EventHandler<ContractFailedEventArgs> InternalContractFailedEvent {
51 get { return ContractFailed; }
55 internal static Type GetContractExceptionType ()
57 return typeof (ContractException);
61 internal static Type GetContractShouldAssertExceptionType ()
63 return typeof (ContractShouldAssertException);
66 static void ReportFailure (ContractFailureKind kind, string userMessage, string conditionText, Exception innerException)
68 string msg = ContractHelper.RaiseContractFailedEvent (kind, userMessage, conditionText, innerException);
69 // if msg is null, then it has been handled already, so don't do anything here
71 ContractHelper.TriggerFailure (kind, msg, userMessage, conditionText, innerException);
74 static void AssertMustUseRewriter (ContractFailureKind kind, string message)
76 ContractHelper.TriggerFailure (kind, "Assembly must be re-written by the code contract rewritter", null, message, null);
79 [ConditionalAttribute("CONTRACTS_FULL")]
80 [ConditionalAttribute("DEBUG")]
81 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
82 public static void Assert (bool condition)
87 ReportFailure (ContractFailureKind.Assert, null, null, null);
90 [ConditionalAttribute("DEBUG")]
91 [ConditionalAttribute("CONTRACTS_FULL")]
92 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
93 public static void Assert (bool condition, string userMessage)
98 ReportFailure (ContractFailureKind.Assert, userMessage, null, null);
101 [ConditionalAttribute("DEBUG")]
102 [ConditionalAttribute("CONTRACTS_FULL")]
103 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
104 public static void Assume(bool condition)
106 // At runtime, this behaves like assert
110 ReportFailure (ContractFailureKind.Assume, null, null, null);
113 [ConditionalAttribute("CONTRACTS_FULL")]
114 [ConditionalAttribute("DEBUG")]
115 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
116 public static void Assume (bool condition, string userMessage)
118 // At runtime, this behaves like assert
122 ReportFailure (ContractFailureKind.Assume, userMessage, null, null);
125 [ConditionalAttribute("CONTRACTS_FULL")]
126 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
127 public static void EndContractBlock ()
129 // Marker method, no code required.
132 [ConditionalAttribute("CONTRACTS_FULL")]
133 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
134 public static void Ensures (bool condition)
136 AssertMustUseRewriter (ContractFailureKind.Postcondition, "Contract.Ensures");
139 [ConditionalAttribute("CONTRACTS_FULL")]
140 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
141 public static void Ensures (bool condition, string userMessage)
143 AssertMustUseRewriter (ContractFailureKind.Postcondition, "Contract.Ensures");
146 [ConditionalAttribute("CONTRACTS_FULL")]
147 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
148 public static void EnsuresOnThrow<TException> (bool condition) where TException : Exception
150 AssertMustUseRewriter (ContractFailureKind.Postcondition, "Contract.EnsuresOnThrow");
153 [ConditionalAttribute("CONTRACTS_FULL")]
154 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
155 public static void EnsuresOnThrow<TException> (bool condition, string userMessage) where TException : Exception
157 AssertMustUseRewriter (ContractFailureKind.Postcondition, "Contract.EnsuresOnThrow");
160 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
161 public static bool Exists<T> (IEnumerable<T> collection, Predicate<T> predicate)
163 if (predicate == null)
164 throw new ArgumentNullException ("predicate");
165 if (collection == null)
166 throw new ArgumentNullException ("collection");
168 foreach (var t in collection)
174 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
175 public static bool Exists (int fromInclusive, int toExclusive, Predicate<int> predicate)
177 if (predicate == null)
178 throw new ArgumentNullException ("predicate");
179 if (toExclusive < fromInclusive)
180 throw new ArgumentException ("toExclusitve < fromInclusive");
182 for (int i = fromInclusive; i < toExclusive; i++)
189 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
190 public static bool ForAll<T> (IEnumerable<T> collection, Predicate<T> predicate)
192 if (predicate == null)
193 throw new ArgumentNullException ("predicate");
194 if (collection == null)
195 throw new ArgumentNullException ("collection");
197 foreach (var t in collection)
204 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
205 public static bool ForAll (int fromInclusive, int toExclusive, Predicate<int> predicate)
207 if (predicate == null)
208 throw new ArgumentNullException ("predicate");
209 if (toExclusive < fromInclusive)
210 throw new ArgumentException ("toExclusitve < fromInclusive");
212 for (int i = fromInclusive; i < toExclusive; i++)
219 [ConditionalAttribute("CONTRACTS_FULL")]
220 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
221 public static void Invariant (bool condition)
223 AssertMustUseRewriter (ContractFailureKind.Invariant, "Contract.Invariant");
226 [ConditionalAttribute("CONTRACTS_FULL")]
227 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
228 public static void Invariant (bool condition, string userMessage)
230 AssertMustUseRewriter (ContractFailureKind.Invariant, "Contract.Invariant");
233 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
234 public static T OldValue<T> (T value)
236 // Marker method, no code required.
240 [ConditionalAttribute("CONTRACTS_FULL")]
241 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
242 public static void Requires (bool condition)
244 AssertMustUseRewriter (ContractFailureKind.Precondition, "Contract.Requires");
247 [ConditionalAttribute("CONTRACTS_FULL")]
248 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
249 public static void Requires (bool condition, string userMessage)
251 AssertMustUseRewriter (ContractFailureKind.Precondition, "Contract.Requires");
254 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
255 public static void Requires<TException> (bool condition) where TException : Exception
257 AssertMustUseRewriter (ContractFailureKind.Precondition, "Contract.Requires<TException>");
260 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
261 public static void Requires<TException> (bool condition, string userMessage) where TException : Exception
263 AssertMustUseRewriter (ContractFailureKind.Precondition, "Contract.Requires<TException>");
266 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
267 public static T Result<T> ()
269 // Marker method, no code required.
273 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
274 public static T ValueAtReturn<T> (out T value)
276 // Marker method, no code required.
277 return value = default (T);