1 /* ****************************************************************************
3 * Copyright (c) Microsoft Corporation.
5 * This source code is subject to terms and conditions of the Microsoft Public License. A
6 * copy of the license can be found in the License.html file at the root of this distribution. If
7 * you cannot locate the Microsoft Public License, please send an email to
8 * dlr@microsoft.com. By using this source code in any fashion, you are agreeing to be bound
9 * by the terms of the Microsoft Public License.
11 * You must not remove this notice, or any other, from this software.
14 * ***************************************************************************/
15 using System; using Microsoft;
18 using System.Collections.Generic;
19 using System.Diagnostics;
21 using System.Linq.Expressions;
23 using Microsoft.Linq.Expressions;
27 namespace System.Dynamic.Utils {
29 namespace Microsoft.Scripting.Utils {
32 // Will be replaced with CLRv4 managed contracts
33 internal static class ContractUtils {
35 internal static Exception Unreachable {
37 Debug.Assert(false, "Unreachable");
38 return new InvalidOperationException("Code supposed to be unreachable");
42 internal static void Requires(bool precondition) {
44 throw new ArgumentException(Strings.MethodPreconditionViolated);
48 internal static void Requires(bool precondition, string paramName) {
49 Debug.Assert(!string.IsNullOrEmpty(paramName));
52 throw new ArgumentException(Strings.InvalidArgumentValue, paramName);
56 internal static void Requires(bool precondition, string paramName, string message) {
57 Debug.Assert(!string.IsNullOrEmpty(paramName));
60 throw new ArgumentException(message, paramName);
64 internal static void RequiresNotNull(object value, string paramName) {
65 Debug.Assert(!string.IsNullOrEmpty(paramName));
68 throw new ArgumentNullException(paramName);
72 internal static void RequiresNotEmpty<T>(ICollection<T> collection, string paramName) {
73 RequiresNotNull(collection, paramName);
74 if (collection.Count == 0) {
75 throw new ArgumentException(Strings.NonEmptyCollectionRequired, paramName);
80 /// Requires the range [offset, offset + count] to be a subset of [0, array.Count].
82 /// <exception cref="ArgumentNullException">Array is <c>null</c>.</exception>
83 /// <exception cref="ArgumentOutOfRangeException">Offset or count are out of range.</exception>
84 internal static void RequiresArrayRange<T>(IList<T> array, int offset, int count, string offsetName, string countName) {
85 Debug.Assert(!string.IsNullOrEmpty(offsetName));
86 Debug.Assert(!string.IsNullOrEmpty(countName));
87 Debug.Assert(array != null);
89 if (count < 0) throw new ArgumentOutOfRangeException(countName);
90 if (offset < 0 || array.Count - offset < count) throw new ArgumentOutOfRangeException(offsetName);
94 /// Requires the array and all its items to be non-null.
96 internal static void RequiresNotNullItems<T>(IList<T> array, string arrayName) {
97 Debug.Assert(arrayName != null);
98 RequiresNotNull(array, arrayName);
100 for (int i = 0; i < array.Count; i++) {
101 if (array[i] == null) {
102 throw new ArgumentNullException(string.Format(System.Globalization.CultureInfo.CurrentCulture, "{0}[{1}]", arrayName, i));