Verify.cs 8.3 KB

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