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.

133 lines
3.7 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Text.RegularExpressions;
  5. using System.Threading;
  6. namespace Shentun.Utilities
  7. {
  8. public class ArgumentValidation
  9. {
  10. #region 判断对象是否为空
  11. /// <summary>
  12. /// 判断对象是否为空,为空返回true
  13. /// </summary>
  14. /// <typeparam name="T">要验证的对象的类型</typeparam>
  15. /// <param name="data">要验证的对象</param>
  16. public static bool IsNull<T>(T data)
  17. {
  18. //如果为null
  19. if (data == null)
  20. {
  21. return true;
  22. }
  23. //如果为""
  24. if (data.GetType() == typeof(String))
  25. {
  26. if (string.IsNullOrWhiteSpace(data.ToString().Trim()))
  27. {
  28. return true;
  29. }
  30. }
  31. //如果为DBNull
  32. if (data.GetType() == typeof(DBNull))
  33. {
  34. return true;
  35. }
  36. //不为空
  37. return false;
  38. }
  39. /// <summary>
  40. /// 判断对象是否为空,为空返回true
  41. /// </summary>
  42. /// <param name="data">要验证的对象</param>
  43. public static bool IsNull(object data)
  44. {
  45. //如果为null
  46. if (data == null)
  47. {
  48. return true;
  49. }
  50. //如果为""
  51. if (data.GetType() == typeof(String))
  52. {
  53. if (string.IsNullOrWhiteSpace(data.ToString().Trim()))
  54. {
  55. return true;
  56. }
  57. }
  58. //如果为DBNull
  59. if (data.GetType() == typeof(DBNull))
  60. {
  61. return true;
  62. }
  63. //不为空
  64. return false;
  65. }
  66. public static void CheckNull<T>(T data, string variableName)
  67. {
  68. if (IsNull<T>(data))
  69. {
  70. throw new Exception(variableName + "不能为空");
  71. }
  72. if(data is DateTime)
  73. {
  74. DateTime? dateTime ;
  75. dateTime = data as DateTime?;
  76. if (dateTime <= new DateTime(1900,1,1))
  77. {
  78. throw new Exception(variableName + "不能为空");
  79. }
  80. }
  81. }
  82. public static bool IsNumber(string value)
  83. {
  84. try
  85. {
  86. decimal data = 0.0m;
  87. if(value.ToUpper().Contains("E"))
  88. {
  89. data = Convert.ToDecimal(Decimal.Parse(value.ToString(), System.Globalization.NumberStyles.Float));
  90. }
  91. else
  92. {
  93. data = Convert.ToDecimal(value);
  94. }
  95. return true;
  96. }
  97. catch(Exception ex)
  98. {
  99. return false;
  100. }
  101. //return Regex.IsMatch(value, "[\\+-]?[0-9]*(\\.[0-9])?([eE][\\+-]?[0-9]+)?");
  102. }
  103. public static void CheckIdNo(string idNo)
  104. {
  105. if(string.IsNullOrWhiteSpace(idNo))
  106. {
  107. throw new Exception("身份证号不能是空值");
  108. }
  109. if (idNo.Length != 18)
  110. throw new Exception("身份证号长度必须为18位");
  111. var birthDate = Convert.ToDateTime(idNo.Substring(6, 4) + "-" +
  112. idNo.Substring(10, 2) + "-" +
  113. idNo.Substring(12, 2));
  114. if (birthDate < new DateTime(1880, 1, 1) || birthDate > DateTime.Now.Date)
  115. {
  116. throw new Exception("身份证号中的出生日期不正确");
  117. }
  118. }
  119. #endregion
  120. }
  121. }