PageRenderTime 50ms CodeModel.GetById 8ms RepoModel.GetById 1ms app.codeStats 0ms

/Microsoft.Windows.Shell/Standard/Verify.cs

https://github.com/josephcooney/ILSpy
C# | 312 lines | 219 code | 24 blank | 69 comment | 46 complexity | c1b4c312c14710afece9c08130174cbc MD5 | raw file
  1. /**************************************************************************\
  2. Copyright Microsoft Corporation. All Rights Reserved.
  3. \**************************************************************************/
  4. // This file contains general utilities to aid in development.
  5. // Classes here generally shouldn't be exposed publicly since
  6. // they're not particular to any library functionality.
  7. // Because the classes here are internal, it's likely this file
  8. // might be included in multiple assemblies.
  9. namespace Standard
  10. {
  11. using System;
  12. using System.Diagnostics;
  13. using System.Diagnostics.CodeAnalysis;
  14. using System.Globalization;
  15. using System.IO;
  16. using System.Threading;
  17. /// <summary>
  18. /// A static class for retail validated assertions.
  19. /// Instead of breaking into the debugger an exception is thrown.
  20. /// </summary>
  21. internal static class Verify
  22. {
  23. /// <summary>
  24. /// Ensure that the current thread's apartment state is what's expected.
  25. /// </summary>
  26. /// <param name="requiredState">
  27. /// The required apartment state for the current thread.
  28. /// </param>
  29. /// <param name="message">
  30. /// The message string for the exception to be thrown if the state is invalid.
  31. /// </param>
  32. /// <exception cref="InvalidOperationException">
  33. /// Thrown if the calling thread's apartment state is not the same as the requiredState.
  34. /// </exception>
  35. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  36. [DebuggerStepThrough]
  37. public static void IsApartmentState(ApartmentState requiredState, string message)
  38. {
  39. if (Thread.CurrentThread.GetApartmentState() != requiredState)
  40. {
  41. throw new InvalidOperationException(message);
  42. }
  43. }
  44. /// <summary>
  45. /// Ensure that an argument is neither null nor empty.
  46. /// </summary>
  47. /// <param name="value">The string to validate.</param>
  48. /// <param name="name">The name of the parameter that will be presented if an exception is thrown.</param>
  49. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  50. [SuppressMessage("Microsoft.Performance", "CA1820:TestForEmptyStringsUsingStringLength")]
  51. [DebuggerStepThrough]
  52. public static void IsNeitherNullNorEmpty(string value, string name)
  53. {
  54. // catch caller errors, mixing up the parameters. Name should never be empty.
  55. Assert.IsNeitherNullNorEmpty(name);
  56. // Notice that ArgumentNullException and ArgumentException take the parameters in opposite order :P
  57. const string errorMessage = "The parameter can not be either null or empty.";
  58. if (null == value)
  59. {
  60. throw new ArgumentNullException(name, errorMessage);
  61. }
  62. if ("" == value)
  63. {
  64. throw new ArgumentException(errorMessage, name);
  65. }
  66. }
  67. /// <summary>
  68. /// Ensure that an argument is neither null nor does it consist only of whitespace.
  69. /// </summary>
  70. /// <param name="value">The string to validate.</param>
  71. /// <param name="name">The name of the parameter that will be presented if an exception is thrown.</param>
  72. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  73. [SuppressMessage("Microsoft.Performance", "CA1820:TestForEmptyStringsUsingStringLength")]
  74. [DebuggerStepThrough]
  75. public static void IsNeitherNullNorWhitespace(string value, string name)
  76. {
  77. // catch caller errors, mixing up the parameters. Name should never be empty.
  78. Assert.IsNeitherNullNorEmpty(name);
  79. // Notice that ArgumentNullException and ArgumentException take the parameters in opposite order :P
  80. const string errorMessage = "The parameter can not be either null or empty or consist only of white space characters.";
  81. if (null == value)
  82. {
  83. throw new ArgumentNullException(name, errorMessage);
  84. }
  85. if ("" == value.Trim())
  86. {
  87. throw new ArgumentException(errorMessage, name);
  88. }
  89. }
  90. /// <summary>Verifies that an argument is not null.</summary>
  91. /// <typeparam name="T">Type of the object to validate. Must be a class.</typeparam>
  92. /// <param name="obj">The object to validate.</param>
  93. /// <param name="name">The name of the parameter that will be presented if an exception is thrown.</param>
  94. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  95. [DebuggerStepThrough]
  96. public static void IsNotDefault<T>(T obj, string name) where T : struct
  97. {
  98. if (default(T).Equals(obj))
  99. {
  100. throw new ArgumentException("The parameter must not be the default value.", name);
  101. }
  102. }
  103. /// <summary>Verifies that an argument is not null.</summary>
  104. /// <typeparam name="T">Type of the object to validate. Must be a class.</typeparam>
  105. /// <param name="obj">The object to validate.</param>
  106. /// <param name="name">The name of the parameter that will be presented if an exception is thrown.</param>
  107. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  108. [DebuggerStepThrough]
  109. public static void IsNotNull<T>(T obj, string name) where T : class
  110. {
  111. if (null == obj)
  112. {
  113. throw new ArgumentNullException(name);
  114. }
  115. }
  116. /// <summary>Verifies that an argument is null.</summary>
  117. /// <typeparam name="T">Type of the object to validate. Must be a class.</typeparam>
  118. /// <param name="obj">The object to validate.</param>
  119. /// <param name="name">The name of the parameter that will be presented if an exception is thrown.</param>
  120. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  121. [DebuggerStepThrough]
  122. public static void IsNull<T>(T obj, string name) where T : class
  123. {
  124. if (null != obj)
  125. {
  126. throw new ArgumentException("The parameter must be null.", name);
  127. }
  128. }
  129. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  130. [DebuggerStepThrough]
  131. public static void PropertyIsNotNull<T>(T obj, string name) where T : class
  132. {
  133. if (null == obj)
  134. {
  135. throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "The property {0} cannot be null at this time.", name));
  136. }
  137. }
  138. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  139. [DebuggerStepThrough]
  140. public static void PropertyIsNull<T>(T obj, string name) where T : class
  141. {
  142. if (null != obj)
  143. {
  144. throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "The property {0} must be null at this time.", name));
  145. }
  146. }
  147. /// <summary>
  148. /// Verifies the specified statement is true. Throws an ArgumentException if it's not.
  149. /// </summary>
  150. /// <param name="statement">The statement to be verified as true.</param>
  151. /// <param name="name">Name of the parameter to include in the ArgumentException.</param>
  152. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  153. [DebuggerStepThrough]
  154. public static void IsTrue(bool statement, string name)
  155. {
  156. if (!statement)
  157. {
  158. throw new ArgumentException("", name);
  159. }
  160. }
  161. /// <summary>
  162. /// Verifies the specified statement is true. Throws an ArgumentException if it's not.
  163. /// </summary>
  164. /// <param name="statement">The statement to be verified as true.</param>
  165. /// <param name="name">Name of the parameter to include in the ArgumentException.</param>
  166. /// <param name="message">The message to include in the ArgumentException.</param>
  167. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  168. [DebuggerStepThrough]
  169. public static void IsTrue(bool statement, string name, string message)
  170. {
  171. if (!statement)
  172. {
  173. throw new ArgumentException(message, name);
  174. }
  175. }
  176. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  177. [DebuggerStepThrough]
  178. public static void AreEqual<T>(T expected, T actual, string parameterName, string message)
  179. {
  180. if (null == expected)
  181. {
  182. // Two nulls are considered equal, regardless of type semantics.
  183. if (null != actual && !actual.Equals(expected))
  184. {
  185. throw new ArgumentException(message, parameterName);
  186. }
  187. }
  188. else if (!expected.Equals(actual))
  189. {
  190. throw new ArgumentException(message, parameterName);
  191. }
  192. }
  193. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  194. [DebuggerStepThrough]
  195. public static void AreNotEqual<T>(T notExpected, T actual, string parameterName, string message)
  196. {
  197. if (null == notExpected)
  198. {
  199. // Two nulls are considered equal, regardless of type semantics.
  200. if (null == actual || actual.Equals(notExpected))
  201. {
  202. throw new ArgumentException(message, parameterName);
  203. }
  204. }
  205. else if (notExpected.Equals(actual))
  206. {
  207. throw new ArgumentException(message, parameterName);
  208. }
  209. }
  210. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  211. [DebuggerStepThrough]
  212. public static void UriIsAbsolute(Uri uri, string parameterName)
  213. {
  214. Verify.IsNotNull(uri, parameterName);
  215. if (!uri.IsAbsoluteUri)
  216. {
  217. throw new ArgumentException("The URI must be absolute.", parameterName);
  218. }
  219. }
  220. /// <summary>
  221. /// Verifies that the specified value is within the expected range. The assertion fails if it isn't.
  222. /// </summary>
  223. /// <param name="lowerBoundInclusive">The lower bound inclusive value.</param>
  224. /// <param name="value">The value to verify.</param>
  225. /// <param name="upperBoundExclusive">The upper bound exclusive value.</param>
  226. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  227. [DebuggerStepThrough]
  228. public static void BoundedInteger(int lowerBoundInclusive, int value, int upperBoundExclusive, string parameterName)
  229. {
  230. if (value < lowerBoundInclusive || value >= upperBoundExclusive)
  231. {
  232. throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, "The integer value must be bounded with [{0}, {1})", lowerBoundInclusive, upperBoundExclusive), parameterName);
  233. }
  234. }
  235. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  236. [DebuggerStepThrough]
  237. public static void BoundedDoubleInc(double lowerBoundInclusive, double value, double upperBoundInclusive, string message, string parameter)
  238. {
  239. if (value < lowerBoundInclusive || value > upperBoundInclusive)
  240. {
  241. throw new ArgumentException(message, parameter);
  242. }
  243. }
  244. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  245. [DebuggerStepThrough]
  246. public static void TypeSupportsInterface(Type type, Type interfaceType, string parameterName)
  247. {
  248. Assert.IsNeitherNullNorEmpty(parameterName);
  249. Verify.IsNotNull(type, "type");
  250. Verify.IsNotNull(interfaceType, "interfaceType");
  251. if (type.GetInterface(interfaceType.Name) == null)
  252. {
  253. throw new ArgumentException("The type of this parameter does not support a required interface", parameterName);
  254. }
  255. }
  256. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  257. [DebuggerStepThrough]
  258. public static void FileExists(string filePath, string parameterName)
  259. {
  260. Verify.IsNeitherNullNorEmpty(filePath, parameterName);
  261. if (!File.Exists(filePath))
  262. {
  263. throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, "No file exists at \"{0}\"", filePath), parameterName);
  264. }
  265. }
  266. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  267. [DebuggerStepThrough]
  268. internal static void ImplementsInterface(object parameter, Type interfaceType, string parameterName)
  269. {
  270. Assert.IsNotNull(parameter);
  271. Assert.IsNotNull(interfaceType);
  272. Assert.IsTrue(interfaceType.IsInterface);
  273. bool isImplemented = false;
  274. foreach (var ifaceType in parameter.GetType().GetInterfaces())
  275. {
  276. if (ifaceType == interfaceType)
  277. {
  278. isImplemented = true;
  279. break;
  280. }
  281. }
  282. if (!isImplemented)
  283. {
  284. throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, "The parameter must implement interface {0}.", interfaceType.ToString()), parameterName);
  285. }
  286. }
  287. }
  288. }