NullHelper.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. using System;
  2. namespace EasyDevCore.Common
  3. {
  4. /// <summary>
  5. ///
  6. /// </summary>
  7. public static class NullHelper
  8. {
  9. /// <summary>
  10. /// Default value of DBNull
  11. /// </summary>
  12. /// <param name="type"></param>
  13. /// <returns></returns>
  14. internal static object DefaultValue(Type type)
  15. {
  16. if (type.Equals(typeof(byte)) || type.Equals(typeof(Int16)) || type.Equals(typeof(int)) || type.Equals(typeof(Int64))
  17. || type.Equals(typeof(ushort)) || type.Equals(typeof(uint)) || type.Equals(typeof(UInt16)) || type.Equals(typeof(UInt32)) || type.Equals(typeof(UInt64)))
  18. {
  19. return (byte)0;
  20. }
  21. else if (type.Equals(typeof(Single)))
  22. {
  23. return (Single)0;
  24. }
  25. else if (type.Equals(typeof(double)))
  26. {
  27. return (double)0;
  28. }
  29. else if (type.Equals(typeof(decimal)))
  30. {
  31. return (decimal)0;
  32. }
  33. else if (type.Equals(typeof(DateTime)))
  34. {
  35. return DateTime.MinValue;
  36. }
  37. else if (type.Equals(typeof(string)))
  38. {
  39. return string.Empty;
  40. }
  41. else if (type.Equals(typeof(bool)))
  42. {
  43. return false;
  44. }
  45. else if (type.Equals(typeof(Guid)))
  46. {
  47. return Guid.Empty;
  48. }
  49. else if(type.IsNullable())
  50. {
  51. return null;
  52. }
  53. else
  54. {
  55. throw new NotSupportedException("Type is not supported");
  56. }
  57. }
  58. /// <summary>
  59. /// Replaces NULL with the specified replacement value.
  60. /// </summary>
  61. /// <param name="checkValue">Is the expression to be checked for NULL</param>
  62. /// <param name="replaceValue">Is the expression to be returned if check_value is NULL. replacement_value must have the same type as check_value.</param>
  63. /// <returns>The value of check_value is returned if it is not NULL; otherwise, replacement_value is returned</returns>
  64. public static T IsNull<T>(T checkValue, T replaceValue)
  65. {
  66. if (checkValue == null || Convert.IsDBNull(checkValue))
  67. {
  68. return (T)replaceValue;
  69. }
  70. return checkValue;
  71. }
  72. /// <summary>
  73. /// Return first value is not null
  74. /// </summary>
  75. /// <param name="args">values</param>
  76. /// <returns></returns>
  77. public static T Coalesce<T>(params T[] args)
  78. {
  79. foreach (T value in args)
  80. {
  81. if (value != null && !Convert.IsDBNull(value))
  82. {
  83. return value;
  84. }
  85. }
  86. return default;
  87. }
  88. /// <summary>
  89. /// Convert Null value to default value
  90. /// </summary>
  91. /// <param name="value"></param>
  92. /// <returns></returns>
  93. public static T NullToDefault<T>(T value)
  94. {
  95. #pragma warning disable CS8604 // Possible null reference argument.
  96. return (T)IsNull(value, DefaultValue(typeof(T)));
  97. #pragma warning restore CS8604 // Possible null reference argument.
  98. }
  99. /// <summary>
  100. /// Values if null or empty.
  101. /// </summary>
  102. /// <typeparam name="T"></typeparam>
  103. /// <param name="input">The input.</param>
  104. /// <param name="defaultIfNullOrEmpty">The default if null or empty.</param>
  105. /// <returns></returns>
  106. public static T ValueIfNullOrEmpty<T>(this T input, T defaultIfNullOrEmpty)
  107. {
  108. if (input is string)
  109. {
  110. return string.IsNullOrWhiteSpace(input.ToString()) ? defaultIfNullOrEmpty : input;
  111. }
  112. else
  113. {
  114. return (input is null ? defaultIfNullOrEmpty : input);
  115. }
  116. }
  117. /// <summary>
  118. /// Nulls if value equals checkvalue.
  119. /// </summary>
  120. /// <typeparam name="T"></typeparam>
  121. /// <param name="value">The value.</param>
  122. /// <param name="checkValue">The check value.</param>
  123. /// <returns>null if equals else return value</returns>
  124. public static T NullIf<T>(T value, T checkValue)
  125. {
  126. if (value is null || checkValue is null || object.Equals(value, checkValue))
  127. {
  128. return default(T);
  129. }
  130. return value;
  131. }
  132. }
  133. }