mscorlib(4.0.0.0) API with additions
Contract.cs
2 using System.Reflection;
5 using System.Security;
6 
8 {
10  [__DynamicallyInvokable]
11  public static class Contract
12  {
13  [ThreadStatic]
14  private static bool _assertingMustUseRewriter;
15 
17  [__DynamicallyInvokable]
18  public static event EventHandler<ContractFailedEventArgs> ContractFailed
19  {
20  [SecurityCritical]
21  [__DynamicallyInvokable]
22  add
23  {
24  ContractHelper.InternalContractFailed += value;
25  }
26  [SecurityCritical]
27  [__DynamicallyInvokable]
28  remove
29  {
30  ContractHelper.InternalContractFailed -= value;
31  }
32  }
33 
36  [Conditional("DEBUG")]
37  [Conditional("CONTRACTS_FULL")]
38  [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
39  [__DynamicallyInvokable]
40  public static void Assume(bool condition)
41  {
42  if (!condition)
43  {
44  ReportFailure(ContractFailureKind.Assume, null, null, null);
45  }
46  }
47 
51  [Conditional("DEBUG")]
52  [Conditional("CONTRACTS_FULL")]
53  [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
54  [__DynamicallyInvokable]
55  public static void Assume(bool condition, string userMessage)
56  {
57  if (!condition)
58  {
59  ReportFailure(ContractFailureKind.Assume, userMessage, null, null);
60  }
61  }
62 
65  [Conditional("DEBUG")]
66  [Conditional("CONTRACTS_FULL")]
67  [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
68  [__DynamicallyInvokable]
69  public static void Assert(bool condition)
70  {
71  if (!condition)
72  {
73  ReportFailure(ContractFailureKind.Assert, null, null, null);
74  }
75  }
76 
80  [Conditional("DEBUG")]
81  [Conditional("CONTRACTS_FULL")]
82  [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
83  [__DynamicallyInvokable]
84  public static void Assert(bool condition, string userMessage)
85  {
86  if (!condition)
87  {
88  ReportFailure(ContractFailureKind.Assert, userMessage, null, null);
89  }
90  }
91 
94  [Conditional("CONTRACTS_FULL")]
95  [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
96  [__DynamicallyInvokable]
97  public static void Requires(bool condition)
98  {
99  AssertMustUseRewriter(ContractFailureKind.Precondition, "Requires");
100  }
101 
105  [Conditional("CONTRACTS_FULL")]
106  [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
107  [__DynamicallyInvokable]
108  public static void Requires(bool condition, string userMessage)
109  {
110  AssertMustUseRewriter(ContractFailureKind.Precondition, "Requires");
111  }
112 
116  [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
117  [__DynamicallyInvokable]
118  public static void Requires<TException>(bool condition) where TException : Exception
119  {
120  AssertMustUseRewriter(ContractFailureKind.Precondition, "Requires<TException>");
121  }
122 
127  [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
128  [__DynamicallyInvokable]
129  public static void Requires<TException>(bool condition, string userMessage) where TException : Exception
130  {
131  AssertMustUseRewriter(ContractFailureKind.Precondition, "Requires<TException>");
132  }
133 
136  [Conditional("CONTRACTS_FULL")]
137  [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
138  [__DynamicallyInvokable]
139  public static void Ensures(bool condition)
140  {
141  AssertMustUseRewriter(ContractFailureKind.Postcondition, "Ensures");
142  }
143 
147  [Conditional("CONTRACTS_FULL")]
148  [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
149  [__DynamicallyInvokable]
150  public static void Ensures(bool condition, string userMessage)
151  {
152  AssertMustUseRewriter(ContractFailureKind.Postcondition, "Ensures");
153  }
154 
158  [Conditional("CONTRACTS_FULL")]
159  [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
160  [__DynamicallyInvokable]
161  public static void EnsuresOnThrow<TException>(bool condition) where TException : Exception
162  {
163  AssertMustUseRewriter(ContractFailureKind.PostconditionOnException, "EnsuresOnThrow");
164  }
165 
170  [Conditional("CONTRACTS_FULL")]
171  [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
172  [__DynamicallyInvokable]
173  public static void EnsuresOnThrow<TException>(bool condition, string userMessage) where TException : Exception
174  {
175  AssertMustUseRewriter(ContractFailureKind.PostconditionOnException, "EnsuresOnThrow");
176  }
177 
181  [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
182  [__DynamicallyInvokable]
183  public static T Result<T>()
184  {
185  return default(T);
186  }
187 
192  [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
193  [__DynamicallyInvokable]
194  public static T ValueAtReturn<T>(out T value)
195  {
196  value = default(T);
197  return value;
198  }
199 
204  [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
205  [__DynamicallyInvokable]
206  public static T OldValue<T>(T value)
207  {
208  return default(T);
209  }
210 
213  [Conditional("CONTRACTS_FULL")]
214  [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
215  [__DynamicallyInvokable]
216  public static void Invariant(bool condition)
217  {
218  AssertMustUseRewriter(ContractFailureKind.Invariant, "Invariant");
219  }
220 
224  [Conditional("CONTRACTS_FULL")]
225  [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
226  [__DynamicallyInvokable]
227  public static void Invariant(bool condition, string userMessage)
228  {
229  AssertMustUseRewriter(ContractFailureKind.Invariant, "Invariant");
230  }
231 
242  [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
243  [__DynamicallyInvokable]
244  public static bool ForAll(int fromInclusive, int toExclusive, Predicate<int> predicate)
245  {
246  if (fromInclusive > toExclusive)
247  {
248  throw new ArgumentException(Environment.GetResourceString("Argument_ToExclusiveLessThanFromExclusive"));
249  }
250  if (predicate == null)
251  {
252  throw new ArgumentNullException("predicate");
253  }
254  for (int i = fromInclusive; i < toExclusive; i++)
255  {
256  if (!predicate(i))
257  {
258  return false;
259  }
260  }
261  return true;
262  }
263 
272  [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
273  [__DynamicallyInvokable]
274  public static bool ForAll<T>(IEnumerable<T> collection, Predicate<T> predicate)
275  {
276  if (collection == null)
277  {
278  throw new ArgumentNullException("collection");
279  }
280  if (predicate == null)
281  {
282  throw new ArgumentNullException("predicate");
283  }
284  foreach (T item in collection)
285  {
286  if (!predicate(item))
287  {
288  return false;
289  }
290  }
291  return true;
292  }
293 
304  [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
305  [__DynamicallyInvokable]
306  public static bool Exists(int fromInclusive, int toExclusive, Predicate<int> predicate)
307  {
308  if (fromInclusive > toExclusive)
309  {
310  throw new ArgumentException(Environment.GetResourceString("Argument_ToExclusiveLessThanFromExclusive"));
311  }
312  if (predicate == null)
313  {
314  throw new ArgumentNullException("predicate");
315  }
316  for (int i = fromInclusive; i < toExclusive; i++)
317  {
318  if (predicate(i))
319  {
320  return true;
321  }
322  }
323  return false;
324  }
325 
334  [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
335  [__DynamicallyInvokable]
336  public static bool Exists<T>(IEnumerable<T> collection, Predicate<T> predicate)
337  {
338  if (collection == null)
339  {
340  throw new ArgumentNullException("collection");
341  }
342  if (predicate == null)
343  {
344  throw new ArgumentNullException("predicate");
345  }
346  foreach (T item in collection)
347  {
348  if (predicate(item))
349  {
350  return true;
351  }
352  }
353  return false;
354  }
355 
357  [Conditional("CONTRACTS_FULL")]
358  [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
359  [__DynamicallyInvokable]
360  public static void EndContractBlock()
361  {
362  }
363 
364  [DebuggerNonUserCode]
365  [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
366  private static void ReportFailure(ContractFailureKind failureKind, string userMessage, string conditionText, Exception innerException)
367  {
368  if (failureKind < ContractFailureKind.Precondition || failureKind > ContractFailureKind.Assume)
369  {
370  throw new ArgumentException(Environment.GetResourceString("Arg_EnumIllegalVal", failureKind), "failureKind");
371  }
372  string text = ContractHelper.RaiseContractFailedEvent(failureKind, userMessage, conditionText, innerException);
373  if (text != null)
374  {
375  ContractHelper.TriggerFailure(failureKind, text, userMessage, conditionText, innerException);
376  }
377  }
378 
379  [SecuritySafeCritical]
380  private static void AssertMustUseRewriter(ContractFailureKind kind, string contractKind)
381  {
382  if (_assertingMustUseRewriter)
383  {
384  System.Diagnostics.Assert.Fail("Asserting that we must use the rewriter went reentrant.", "Didn't rewrite this mscorlib?");
385  }
386  _assertingMustUseRewriter = true;
387  Assembly assembly = typeof(Contract).Assembly;
388  StackTrace stackTrace = new StackTrace();
389  Assembly assembly2 = null;
390  for (int i = 0; i < stackTrace.FrameCount; i++)
391  {
392  Assembly assembly3 = stackTrace.GetFrame(i).GetMethod().DeclaringType.Assembly;
393  if (assembly3 != assembly)
394  {
395  assembly2 = assembly3;
396  break;
397  }
398  }
399  if (assembly2 == null)
400  {
401  assembly2 = assembly;
402  }
403  string name = assembly2.GetName().Name;
404  ContractHelper.TriggerFailure(kind, Environment.GetResourceString("MustUseCCRewrite", contractKind, name), null, null, null);
405  _assertingMustUseRewriter = false;
406  }
407  }
408 }
static void Requires(bool condition)
Specifies a precondition contract for the enclosing method or property.
Definition: Contract.cs:97
The exception that is thrown when a null reference (Nothing in Visual Basic) is passed to a method th...
static void Invariant(bool condition)
Specifies an invariant contract for the enclosing method or property.
Definition: Contract.cs:216
static T ValueAtReturn< T >(out T value)
Represents the final (output) value of an out parameter when returning from a method.
Definition: Contract.cs:194
static void Assume(bool condition, string userMessage)
Instructs code analysis tools to assume that a condition is true, even if it cannot be statically pro...
Definition: Contract.cs:55
static void EnsuresOnThrow< TException >(bool condition)
Specifies a postcondition contract for the enclosing method or property, based on the provided except...
Definition: Contract.cs:161
virtual AssemblyName GetName()
Gets an T:System.Reflection.AssemblyName for this assembly.
Definition: Assembly.cs:832
Definition: __Canon.cs:3
static T OldValue< T >(T value)
Represents values as they were at the start of a method or property.
Definition: Contract.cs:206
static bool Exists(int fromInclusive, int toExclusive, Predicate< int > predicate)
Determines whether a specified test is true for any integer within a range of integers.
Definition: Contract.cs:306
Contains static methods for representing program contracts such as preconditions, postconditions,...
Definition: Contract.cs:11
static void Requires< TException >(bool condition)
Specifies a precondition contract for the enclosing method or property, and throws an exception if th...
Definition: Contract.cs:118
Provides methods that the binary rewriter uses to handle contract failures.
static bool ForAll< T >(IEnumerable< T > collection, Predicate< T > predicate)
Determines whether all the elements in a collection exist within a function.
Definition: Contract.cs:274
Cer
Specifies a method's behavior when called within a constrained execution region.
Definition: Cer.cs:5
Provides information about, and means to manipulate, the current environment and platform....
Definition: Environment.cs:21
static void Assert(bool condition, string userMessage)
Checks for a condition; if the condition is false, follows the escalation policy set by the analyzer ...
Definition: Contract.cs:84
string Name
Gets or sets the simple name of the assembly. This is usually, but not necessarily,...
Definition: AssemblyName.cs:51
static string RaiseContractFailedEvent(ContractFailureKind failureKind, string userMessage, string conditionText, Exception innerException)
Used by the binary rewriter to activate the default failure behavior.
Represents an assembly, which is a reusable, versionable, and self-describing building block of a com...
Definition: Assembly.cs:22
static void EndContractBlock()
Marks the end of the contract section when a method's contracts contain only preconditions in the if-...
Definition: Contract.cs:360
static void Invariant(bool condition, string userMessage)
Specifies an invariant contract for the enclosing method or property, and displays a message if the c...
Definition: Contract.cs:227
static EventHandler< ContractFailedEventArgs > ContractFailed
Occurs when a contract fails.
Definition: Contract.cs:19
static void Ensures(bool condition, string userMessage)
Specifies a postcondition contract for a provided exit condition and a message to display if the cond...
Definition: Contract.cs:150
static bool ForAll(int fromInclusive, int toExclusive, Predicate< int > predicate)
Determines whether a particular condition is valid for all integers in a specified range.
Definition: Contract.cs:244
static void Assert(bool condition)
Checks for a condition; if the condition is false, follows the escalation policy set for the analyzer...
Definition: Contract.cs:69
The exception that is thrown when one of the arguments provided to a method is not valid.
static bool Exists< T >(IEnumerable< T > collection, Predicate< T > predicate)
Determines whether an element within a collection of elements exists within a function.
Definition: Contract.cs:336
static T Result< T >()
Represents the return value of a method or property.
Definition: Contract.cs:183
Represents errors that occur during application execution.To browse the .NET Framework source code fo...
Definition: Exception.cs:22
Consistency
Specifies a reliability contract.
Definition: Consistency.cs:5
static void TriggerFailure(ContractFailureKind kind, string displayMessage, string userMessage, string conditionText, Exception innerException)
Triggers the default failure behavior.
static void Ensures(bool condition)
Specifies a postcondition contract for the enclosing method or property.
Definition: Contract.cs:139
static void Assume(bool condition)
Instructs code analysis tools to assume that the specified condition is true, even if it cannot be st...
Definition: Contract.cs:40
static void Requires(bool condition, string userMessage)
Specifies a precondition contract for the enclosing method or property, and displays a message if the...
Definition: Contract.cs:108
ContractFailureKind
Specifies the type of contract that failed.