Guard.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. using System;
  2. using System.Diagnostics.CodeAnalysis;
  3. using System.Globalization;
  4. using WindowsPhone.Recipes.Push.Messasges.Properties;
  5. namespace WindowsPhone.Recipes.Push.Messasges
  6. {
  7. /// <summary>
  8. /// A static helper class that includes various parameter checking routines.
  9. /// </summary>
  10. public static partial class Guard
  11. {
  12. /// <summary>
  13. /// Throws <see cref="ArgumentNullException"/> if the given argument is null.
  14. /// </summary>
  15. /// <exception cref="ArgumentNullException"> if tested value if null.</exception>
  16. /// <param name="argumentValue">Argument value to test.</param>
  17. /// <param name="argumentName">Name of the argument being tested.</param>
  18. public static void ArgumentNotNull(object argumentValue,
  19. string argumentName)
  20. {
  21. if (argumentValue == null)
  22. {
  23. throw new ArgumentNullException(argumentName);
  24. }
  25. }
  26. /// <summary>
  27. /// Throws an exception if the tested string argument is null or the empty string.
  28. /// </summary>
  29. /// <exception cref="ArgumentNullException">Thrown if string value is null.</exception>
  30. /// <exception cref="ArgumentException">Thrown if the string is empty</exception>
  31. /// <param name="argumentValue">Argument value to check.</param>
  32. /// <param name="argumentName">Name of argument being checked.</param>
  33. public static void ArgumentNotNullOrEmpty(string argumentValue,
  34. string argumentName)
  35. {
  36. if (argumentValue == null)
  37. {
  38. throw new ArgumentNullException(argumentName);
  39. }
  40. if (argumentValue.Length == 0)
  41. {
  42. throw new ArgumentException(Resources.ArgumentMustNotBeEmpty, argumentName);
  43. }
  44. }
  45. /// <summary>
  46. /// Verifies that an argument type is assignable from the provided type (meaning
  47. /// interfaces are implemented, or classes exist in the base class hierarchy).
  48. /// </summary>
  49. /// <param name="assignmentTargetType">The argument type that will be assigned to.</param>
  50. /// <param name="assignmentValueType">The type of the value being assigned.</param>
  51. /// <param name="argumentName">Argument name.</param>
  52. public static void TypeIsAssignable(Type assignmentTargetType, Type assignmentValueType, string argumentName)
  53. {
  54. if (assignmentTargetType == null)
  55. {
  56. throw new ArgumentNullException("assignmentTargetType");
  57. }
  58. if (assignmentValueType == null)
  59. {
  60. throw new ArgumentNullException("assignmentValueType");
  61. }
  62. if (!assignmentTargetType.IsAssignableFrom(assignmentValueType))
  63. {
  64. throw new ArgumentException(string.Format(
  65. CultureInfo.CurrentCulture,
  66. Resources.TypesAreNotAssignable,
  67. assignmentTargetType,
  68. assignmentValueType),
  69. argumentName);
  70. }
  71. }
  72. /// <summary>
  73. /// Verifies that an argument instance is assignable from the provided type (meaning
  74. /// interfaces are implemented, or classes exist in the base class hierarchy, or instance can be
  75. /// assigned through a runtime wrapper, as is the case for COM Objects).
  76. /// </summary>
  77. /// <param name="assignmentTargetType">The argument type that will be assigned to.</param>
  78. /// <param name="assignmentInstance">The instance that will be assigned.</param>
  79. /// <param name="argumentName">Argument name.</param>
  80. [SuppressMessage(
  81. "Microsoft.Design",
  82. "CA1031:DoNotCatchGeneralExceptionTypes",
  83. Justification = "GetType() invoked for diagnostics purposes")]
  84. public static void InstanceIsAssignable(Type assignmentTargetType, object assignmentInstance, string argumentName)
  85. {
  86. if (assignmentTargetType == null)
  87. {
  88. throw new ArgumentNullException("assignmentTargetType");
  89. }
  90. if (assignmentInstance == null)
  91. {
  92. throw new ArgumentNullException("assignmentInstance");
  93. }
  94. if (!assignmentTargetType.IsInstanceOfType(assignmentInstance))
  95. {
  96. throw new ArgumentException(
  97. string.Format(
  98. CultureInfo.CurrentCulture,
  99. Resources.TypesAreNotAssignable,
  100. assignmentTargetType,
  101. GetTypeName(assignmentInstance)),
  102. argumentName);
  103. }
  104. }
  105. [SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes",
  106. Justification = "Need to use exception as flow control here, no other choice")]
  107. private static string GetTypeName(object assignmentInstance)
  108. {
  109. string assignmentInstanceType;
  110. try
  111. {
  112. assignmentInstanceType = assignmentInstance.GetType().FullName;
  113. }
  114. catch (Exception)
  115. {
  116. assignmentInstanceType = Resources.UnknownType;
  117. }
  118. return assignmentInstanceType;
  119. }
  120. }
  121. }