UtilExtensions.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. using System;
  2. namespace SqlSugar.BzTDengineCore
  3. {
  4. /// <summary>
  5. ///Common Extensions for external users
  6. /// </summary>
  7. public static class UtilExtensions
  8. {
  9. public static string ObjToStringNoTrim(this object thisValue)
  10. {
  11. if (thisValue != null) return thisValue.ToString();
  12. return "";
  13. }
  14. public static string ToLower(this string value, bool isLower)
  15. {
  16. if (isLower)
  17. {
  18. return value.ObjToString().ToLower();
  19. }
  20. return value.ObjToString();
  21. }
  22. public static int ObjToInt(this object thisValue)
  23. {
  24. int reval = 0;
  25. if (thisValue == null) return 0;
  26. if (thisValue is Enum)
  27. {
  28. return (int)thisValue;
  29. }
  30. if (thisValue != null && thisValue != DBNull.Value && int.TryParse(thisValue.ToString(), out reval))
  31. {
  32. return reval;
  33. }
  34. return reval;
  35. }
  36. public static int ObjToInt(this object thisValue, int errorValue)
  37. {
  38. int reval = 0;
  39. if (thisValue is Enum)
  40. {
  41. return (int)thisValue;
  42. }
  43. if (thisValue != null && thisValue != DBNull.Value && int.TryParse(thisValue.ToString(), out reval))
  44. {
  45. return reval;
  46. }
  47. return errorValue;
  48. }
  49. public static double ObjToMoney(this object thisValue)
  50. {
  51. double reval = 0;
  52. if (thisValue != null && thisValue != DBNull.Value && double.TryParse(thisValue.ToString(), out reval))
  53. {
  54. return reval;
  55. }
  56. return 0;
  57. }
  58. public static double ObjToMoney(this object thisValue, double errorValue)
  59. {
  60. double reval = 0;
  61. if (thisValue != null && thisValue != DBNull.Value && double.TryParse(thisValue.ToString(), out reval))
  62. {
  63. return reval;
  64. }
  65. return errorValue;
  66. }
  67. public static string ObjToString(this object thisValue)
  68. {
  69. if (thisValue != null) return thisValue.ToString().Trim();
  70. return "";
  71. }
  72. public static string ObjToString(this object thisValue, string errorValue)
  73. {
  74. if (thisValue != null) return thisValue.ToString().Trim();
  75. return errorValue;
  76. }
  77. public static Decimal ObjToDecimal(this object thisValue)
  78. {
  79. Decimal reval = 0;
  80. if (thisValue != null && thisValue != DBNull.Value && decimal.TryParse(thisValue.ToString(), out reval))
  81. {
  82. return reval;
  83. }
  84. return 0;
  85. }
  86. public static Decimal ObjToDecimal(this object thisValue, decimal errorValue)
  87. {
  88. Decimal reval = 0;
  89. if (thisValue != null && thisValue != DBNull.Value && decimal.TryParse(thisValue.ToString(), out reval))
  90. {
  91. return reval;
  92. }
  93. return errorValue;
  94. }
  95. public static DateTime ObjToDate(this object thisValue)
  96. {
  97. DateTime reval = DateTime.MinValue;
  98. if (thisValue != null && thisValue != DBNull.Value && DateTime.TryParse(thisValue.ToString(), out reval))
  99. {
  100. reval = Convert.ToDateTime(thisValue);
  101. }
  102. return reval;
  103. }
  104. public static DateTime ObjToDate(this object thisValue, DateTime errorValue)
  105. {
  106. DateTime reval = DateTime.MinValue;
  107. if (thisValue != null && thisValue != DBNull.Value && DateTime.TryParse(thisValue.ToString(), out reval))
  108. {
  109. return reval;
  110. }
  111. return errorValue;
  112. }
  113. public static bool ObjToBool(this object thisValue)
  114. {
  115. bool reval = false;
  116. if (thisValue != null && thisValue != DBNull.Value && bool.TryParse(thisValue.ToString(), out reval))
  117. {
  118. return reval;
  119. }
  120. return reval;
  121. }
  122. }
  123. }