|
| 1 | +/* |
| 2 | + * Description:VerifyHelper |
| 3 | + * Author: Chance.Zheng |
| 4 | + * Create Time: 2023-03-07 15:11:57 |
| 5 | + * .Net Version: 4.6 |
| 6 | + * CLR Version: 4.0.30319.42000 |
| 7 | + * Copyright (c) CookCSharp 2023 All Rights Reserved. |
| 8 | + */ |
| 9 | + |
| 10 | + |
| 11 | +using System; |
| 12 | +using System.Collections.Generic; |
| 13 | +using System.Diagnostics; |
| 14 | +using System.Globalization; |
| 15 | +using System.IO; |
| 16 | +using System.Linq; |
| 17 | +using System.Text; |
| 18 | +using System.Threading; |
| 19 | +using System.Threading.Tasks; |
| 20 | + |
| 21 | + |
| 22 | +namespace CookPopularCSharpToolkit.Communal |
| 23 | +{ |
| 24 | + public static class VerifyHelper |
| 25 | + { |
| 26 | + [DebuggerStepThrough] |
| 27 | + public static void IsApartmentState(ApartmentState requiredState, string message) |
| 28 | + { |
| 29 | + if (Thread.CurrentThread.GetApartmentState() != requiredState) |
| 30 | + { |
| 31 | + throw new InvalidOperationException(message); |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + [DebuggerStepThrough] |
| 36 | + public static void IsNeitherNullNorEmpty(string value, string name) |
| 37 | + { |
| 38 | + if (value == null) |
| 39 | + { |
| 40 | + throw new ArgumentNullException(name, "The parameter can not be either null or empty."); |
| 41 | + } |
| 42 | + |
| 43 | + if ("" == value) |
| 44 | + { |
| 45 | + throw new ArgumentException("The parameter can not be either null or empty.", name); |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + [DebuggerStepThrough] |
| 50 | + public static void IsNeitherNullNorWhitespace(string value, string name) |
| 51 | + { |
| 52 | + if (value == null) |
| 53 | + { |
| 54 | + throw new ArgumentNullException(name, "The parameter can not be either null or empty or consist only of white space characters."); |
| 55 | + } |
| 56 | + |
| 57 | + if ("" == value.Trim()) |
| 58 | + { |
| 59 | + throw new ArgumentException("The parameter can not be either null or empty or consist only of white space characters.", name); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + [DebuggerStepThrough] |
| 64 | + public static void IsNotDefault<T>(T obj, string name) where T : struct |
| 65 | + { |
| 66 | + if (default(T).Equals(obj)) |
| 67 | + { |
| 68 | + throw new ArgumentException("The parameter must not be the default value.", name); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + [DebuggerStepThrough] |
| 73 | + public static void IsNotNull<T>(T obj, string name) where T : class |
| 74 | + { |
| 75 | + if (obj == null) |
| 76 | + { |
| 77 | + throw new ArgumentNullException(name); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + [DebuggerStepThrough] |
| 82 | + public static void IsNull<T>(T obj, string name) where T : class |
| 83 | + { |
| 84 | + if (obj != null) |
| 85 | + { |
| 86 | + throw new ArgumentException("The parameter must be null.", name); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + [DebuggerStepThrough] |
| 91 | + public static void PropertyIsNotNull<T>(T obj, string name) where T : class |
| 92 | + { |
| 93 | + if (obj == null) |
| 94 | + { |
| 95 | + throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "The property {0} cannot be null at this time.", new object[1] { name })); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + [DebuggerStepThrough] |
| 100 | + public static void PropertyIsNull<T>(T obj, string name) where T : class |
| 101 | + { |
| 102 | + if (obj != null) |
| 103 | + { |
| 104 | + throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "The property {0} must be null at this time.", new object[1] { name })); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + [DebuggerStepThrough] |
| 109 | + public static void IsTrue(bool statement, string name) |
| 110 | + { |
| 111 | + if (!statement) |
| 112 | + { |
| 113 | + throw new ArgumentException("", name); |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + [DebuggerStepThrough] |
| 118 | + public static void IsTrue(bool statement, string name, string message) |
| 119 | + { |
| 120 | + if (!statement) |
| 121 | + { |
| 122 | + throw new ArgumentException(message, name); |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + [DebuggerStepThrough] |
| 127 | + public static void AreEqual<T>(T expected, T actual, string parameterName, string message) |
| 128 | + { |
| 129 | + if (expected == null) |
| 130 | + { |
| 131 | + if (actual != null && !actual.Equals(expected)) |
| 132 | + { |
| 133 | + throw new ArgumentException(message, parameterName); |
| 134 | + } |
| 135 | + } |
| 136 | + else if (!expected.Equals(actual)) |
| 137 | + { |
| 138 | + throw new ArgumentException(message, parameterName); |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + [DebuggerStepThrough] |
| 143 | + public static void AreNotEqual<T>(T notExpected, T actual, string parameterName, string message) |
| 144 | + { |
| 145 | + if (notExpected == null) |
| 146 | + { |
| 147 | + if (actual == null || actual.Equals(notExpected)) |
| 148 | + { |
| 149 | + throw new ArgumentException(message, parameterName); |
| 150 | + } |
| 151 | + } |
| 152 | + else if (notExpected.Equals(actual)) |
| 153 | + { |
| 154 | + throw new ArgumentException(message, parameterName); |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + [DebuggerStepThrough] |
| 159 | + public static void UriIsAbsolute(Uri uri, string parameterName) |
| 160 | + { |
| 161 | + IsNotNull(uri, parameterName); |
| 162 | + if (!uri.IsAbsoluteUri) |
| 163 | + { |
| 164 | + throw new ArgumentException("The URI must be absolute.", parameterName); |
| 165 | + } |
| 166 | + } |
| 167 | + |
| 168 | + [DebuggerStepThrough] |
| 169 | + public static void BoundedInteger(int lowerBoundInclusive, int value, int upperBoundExclusive, string parameterName) |
| 170 | + { |
| 171 | + if (value < lowerBoundInclusive || value >= upperBoundExclusive) |
| 172 | + { |
| 173 | + throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, "The integer value must be bounded with [{0}, {1})", new object[2] { lowerBoundInclusive, upperBoundExclusive }), parameterName); |
| 174 | + } |
| 175 | + } |
| 176 | + |
| 177 | + [DebuggerStepThrough] |
| 178 | + public static void BoundedDoubleInc(double lowerBoundInclusive, double value, double upperBoundInclusive, string message, string parameter) |
| 179 | + { |
| 180 | + if (value < lowerBoundInclusive || value > upperBoundInclusive) |
| 181 | + { |
| 182 | + throw new ArgumentException(message, parameter); |
| 183 | + } |
| 184 | + } |
| 185 | + |
| 186 | + [DebuggerStepThrough] |
| 187 | + public static void TypeSupportsInterface(Type type, Type interfaceType, string parameterName) |
| 188 | + { |
| 189 | + IsNotNull(type, "type"); |
| 190 | + IsNotNull(interfaceType, "interfaceType"); |
| 191 | + if (type.GetInterface(interfaceType.Name) == null) |
| 192 | + { |
| 193 | + throw new ArgumentException("The type of this parameter does not support a required interface", parameterName); |
| 194 | + } |
| 195 | + } |
| 196 | + |
| 197 | + [DebuggerStepThrough] |
| 198 | + public static void FileExists(string filePath, string parameterName) |
| 199 | + { |
| 200 | + IsNeitherNullNorEmpty(filePath, parameterName); |
| 201 | + if (!File.Exists(filePath)) |
| 202 | + { |
| 203 | + throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, "No file exists at \"{0}\"", new object[1] { filePath }), parameterName); |
| 204 | + } |
| 205 | + } |
| 206 | + |
| 207 | + [DebuggerStepThrough] |
| 208 | + internal static void ImplementsInterface(object parameter, Type interfaceType, string parameterName) |
| 209 | + { |
| 210 | + bool flag = false; |
| 211 | + Type[] interfaces = parameter.GetType().GetInterfaces(); |
| 212 | + foreach (Type type in interfaces) |
| 213 | + { |
| 214 | + if (type == interfaceType) |
| 215 | + { |
| 216 | + flag = true; |
| 217 | + break; |
| 218 | + } |
| 219 | + } |
| 220 | + |
| 221 | + if (!flag) |
| 222 | + { |
| 223 | + throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, "The parameter must implement interface {0}.", new object[1] { interfaceType.ToString() }), parameterName); |
| 224 | + } |
| 225 | + } |
| 226 | + } |
| 227 | +} |
0 commit comments