You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

107 lines
3.6 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace BPA.UIControl.Commons
  7. {
  8. /// <summary>
  9. /// 参数验证
  10. /// </summary>
  11. public sealed class ValidateArgument
  12. {
  13. /// <summary>
  14. /// 非 null
  15. /// </summary>
  16. /// <param name="obj">对象</param>
  17. /// <param name="parameterName">参数名称</param>
  18. /// <exception cref="ArgumentNullException"></exception>
  19. public static void NotNull(object obj, string parameterName)
  20. {
  21. if (obj == null)
  22. {
  23. throw new ArgumentNullException(parameterName);
  24. }
  25. }
  26. /// <summary>
  27. /// 非 null 或空
  28. /// </summary>
  29. /// <param name="parameterValue">字符串值</param>
  30. /// <param name="parameterName">参数名称</param>
  31. /// <exception cref="ArgumentException"></exception>
  32. public static void NotNullOrEmpty(string parameterValue, string parameterName)
  33. {
  34. if (string.IsNullOrEmpty(parameterValue))
  35. {
  36. throw new ArgumentException(parameterName);
  37. }
  38. }
  39. /// <summary>
  40. /// 非 Null 或空类型转换
  41. /// </summary>
  42. /// <typeparam name="T">类型</typeparam>
  43. /// <param name="parameterValue">参数值</param>
  44. /// <param name="parameterName">参数名称</param>
  45. /// <returns></returns>
  46. /// <exception cref="ArgumentException"></exception>
  47. public static T NotNullOrEmptyCast<T>(object parameterValue, string parameterName)
  48. {
  49. ValidateArgument.NotNull(parameterValue, parameterName);
  50. if (parameterValue is T)
  51. {
  52. return (T)((object)parameterValue);
  53. }
  54. throw new ArgumentException(parameterName);
  55. }
  56. /// <summary>
  57. /// 类型转换
  58. /// </summary>
  59. /// <typeparam name="T">类型</typeparam>
  60. /// <param name="parameterValue">参数值</param>
  61. /// <param name="parameterName">参数名称</param>
  62. /// <returns></returns>
  63. /// <exception cref="ArgumentException"></exception>
  64. public static T Cast<T>(object parameterValue, string parameterName)
  65. {
  66. if (parameterValue == null || parameterValue is T)
  67. {
  68. return (T)((object)parameterValue);
  69. }
  70. throw new ArgumentException(parameterName);
  71. }
  72. /// <summary>
  73. /// 是否在范围内
  74. /// </summary>
  75. /// <param name="checkedExpression">检查表达式</param>
  76. /// <param name="parameterName">参数名称</param>
  77. /// <exception cref="ArgumentOutOfRangeException"></exception>
  78. public static void Range(bool checkedExpression, string parameterName)
  79. {
  80. if (!checkedExpression)
  81. {
  82. throw new ArgumentOutOfRangeException(parameterName);
  83. }
  84. }
  85. /// <summary>
  86. /// 是否在范围内
  87. /// </summary>
  88. /// <param name="checkedExpression">检查表达式</param>
  89. /// <param name="parameterName">参数名称</param>
  90. /// <param name="message">错误信息</param>
  91. /// <exception cref="ArgumentOutOfRangeException"></exception>
  92. public static void Range(bool checkedExpression, string parameterName, string message)
  93. {
  94. if (!checkedExpression)
  95. {
  96. throw new ArgumentOutOfRangeException(parameterName, message);
  97. }
  98. }
  99. }
  100. }