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.

187 lines
4.1KB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace GpsCardGatewayPosition.Common
  7. {
  8. public class SafeType
  9. {
  10. /// <summary>
  11. /// Safes the int.
  12. /// </summary>
  13. /// <param name="o">The o.</param>
  14. /// <returns></returns>
  15. public static int SafeInt(object o)
  16. {
  17. var returnValue = 0;
  18. try
  19. {
  20. returnValue = Convert.ToInt32(o);
  21. }
  22. catch
  23. {
  24. returnValue = 0;
  25. }
  26. return returnValue;
  27. }
  28. /// <summary>
  29. /// Safes the int.
  30. /// </summary>
  31. /// <param name="o">The o.</param>
  32. /// <returns></returns>
  33. public static long SafeInt64(object o)
  34. {
  35. var returnValue = 0L;
  36. try
  37. {
  38. returnValue = Convert.ToInt64(o);
  39. }
  40. catch
  41. {
  42. returnValue = 0;
  43. }
  44. return returnValue;
  45. }
  46. /// <summary>
  47. /// Safes the string.
  48. /// </summary>
  49. /// <param name="o">The o.</param>
  50. /// <returns></returns>
  51. public static string SafeString(object o)
  52. {
  53. var returnValue = "";
  54. try
  55. {
  56. returnValue = Convert.ToString(o);
  57. }
  58. catch
  59. {
  60. returnValue = "";
  61. }
  62. return returnValue;
  63. }
  64. /// <summary>
  65. /// Safes the bool.
  66. /// </summary>
  67. /// <param name="o">The o.</param>
  68. /// <returns></returns>
  69. public static bool SafeBool(object o)
  70. {
  71. var returnValue = false;
  72. try
  73. {
  74. returnValue = Convert.ToBoolean(o);
  75. }
  76. catch
  77. {
  78. returnValue = false;
  79. }
  80. return returnValue;
  81. }
  82. public static double SafeDouble(object o)
  83. {
  84. double returnValue = 0;
  85. try
  86. {
  87. returnValue = Convert.ToDouble(o);
  88. }
  89. catch
  90. {
  91. returnValue = 0;
  92. }
  93. return returnValue;
  94. }
  95. /// <summary>
  96. /// Safes the decimal.
  97. /// </summary>
  98. /// <param name="o">The o.</param>
  99. /// <returns></returns>
  100. public static decimal SafeDecimal(object o)
  101. {
  102. var returnValue = 0.0M;
  103. try
  104. {
  105. returnValue = Convert.ToDecimal(o);
  106. }
  107. catch
  108. {
  109. returnValue = 0.0M;
  110. }
  111. return returnValue;
  112. }
  113. /// <summary>
  114. /// Safes the time.
  115. /// </summary>
  116. /// <param name="o">The o.</param>
  117. /// <returns></returns>
  118. public static DateTime SafeTime(object o)
  119. {
  120. var returnValue = DateTime.Parse("1900-01-01");
  121. try
  122. {
  123. returnValue = Convert.ToDateTime(o);
  124. }
  125. catch
  126. {
  127. returnValue = DateTime.Parse("1900-01-01");
  128. }
  129. return returnValue;
  130. }
  131. /// <summary>
  132. /// OBD时间转换
  133. /// </summary>
  134. /// <param name="o"></param>
  135. /// <returns></returns>
  136. public static DateTime OBDSafeTime(object o)
  137. {
  138. var returnValue = DateTime.Parse("1900-01-01");
  139. try
  140. {
  141. returnValue = Convert.ToDateTime(o);
  142. }
  143. catch
  144. {
  145. returnValue = DateTime.UtcNow;
  146. }
  147. return returnValue;
  148. }
  149. /// <summary>
  150. /// 是否为数字
  151. /// </summary>
  152. /// <param name="str"></param>
  153. /// <returns></returns>
  154. public static bool IsNumber(string str)
  155. {
  156. int n;
  157. if (int.TryParse(str, out n))
  158. {
  159. return true;
  160. }
  161. else
  162. {
  163. return false;
  164. }
  165. }
  166. }
  167. }