IcsFromERPJob
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.

376 lines
11 KiB

4 months ago
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace ICSSoft.FromERP
  6. {
  7. public static class ConvertExt
  8. {
  9. public static char ToChar(this object data)
  10. {
  11. char returnValue = default(char);
  12. if (data == null)
  13. return returnValue;
  14. else
  15. {
  16. if (char.TryParse(data.ToString(), out returnValue))
  17. return returnValue;
  18. else
  19. return returnValue;
  20. }
  21. }
  22. #region 文本转换
  23. public static string ToStringExt(this object data)
  24. {
  25. return data == null ? string.Empty : data.ToString();
  26. }
  27. #endregion
  28. #region 日期转换
  29. /// <summary>
  30. /// 转换为日期
  31. /// </summary>
  32. /// <param name="data">数据</param>
  33. /// <returns>DateTime.</returns>
  34. public static DateTime ToDate(this object data)
  35. {
  36. try
  37. {
  38. if (data == null)
  39. return DateTime.MinValue;
  40. if (System.Text.RegularExpressions.Regex.IsMatch(data.ToStringExt(), @"^\d{8}$"))
  41. {
  42. string strValue = data.ToStringExt();
  43. return new DateTime(strValue.Substring(0, 4).ToInt(), strValue.Substring(4, 2).ToInt(), strValue.Substring(6, 2).ToInt());
  44. }
  45. DateTime result;
  46. return DateTime.TryParse(data.ToString(), out result) ? result : DateTime.MinValue;
  47. }
  48. catch
  49. {
  50. return DateTime.MinValue;
  51. }
  52. }
  53. /// <summary>
  54. /// 转换为可空日期
  55. /// </summary>
  56. /// <param name="data">数据</param>
  57. /// <returns>System.Nullable&lt;DateTime&gt;.</returns>
  58. public static DateTime? ToDateOrNull(this object data)
  59. {
  60. try
  61. {
  62. if (data == null)
  63. return null;
  64. if (System.Text.RegularExpressions.Regex.IsMatch(data.ToStringExt(), @"^\d{8}$"))
  65. {
  66. string strValue = data.ToStringExt();
  67. return new DateTime(strValue.Substring(0, 4).ToInt(), strValue.Substring(4, 2).ToInt(), strValue.Substring(6, 2).ToInt());
  68. }
  69. DateTime result;
  70. bool isValid = DateTime.TryParse(data.ToString(), out result);
  71. if (isValid)
  72. return result;
  73. return null;
  74. }
  75. catch
  76. {
  77. return null;
  78. }
  79. }
  80. /// <summary>
  81. /// 转换为日期
  82. /// </summary>
  83. /// <param name="data">数据</param>
  84. /// <returns>DateTime.</returns>
  85. public static DateTime ToDateTime(this object data)
  86. {
  87. try
  88. {
  89. if (data == null)
  90. return DateTime.MinValue;
  91. if (System.Text.RegularExpressions.Regex.IsMatch(data.ToStringExt(), @"^\d{14}$"))
  92. {
  93. string strValue = data.ToStringExt();
  94. return new DateTime(strValue.Substring(0, 4).ToInt(), strValue.Substring(4, 2).ToInt(), strValue.Substring(6, 2).ToInt(),
  95. strValue.Substring(8, 2).ToInt(), strValue.Substring(10, 2).ToInt(), strValue.Substring(12, 2).ToInt());
  96. }
  97. DateTime result;
  98. return DateTime.TryParse(data.ToString(), out result) ? result : DateTime.MinValue;
  99. }
  100. catch
  101. {
  102. return DateTime.MinValue;
  103. }
  104. }
  105. /// <summary>
  106. /// 转换为可空日期
  107. /// </summary>
  108. /// <param name="data">数据</param>
  109. /// <returns>System.Nullable&lt;DateTime&gt;.</returns>
  110. public static DateTime? ToDateTimeOrNull(this object data)
  111. {
  112. try
  113. {
  114. if (data == null)
  115. return null;
  116. if (System.Text.RegularExpressions.Regex.IsMatch(data.ToStringExt(), @"^\d{14}$"))
  117. {
  118. string strValue = data.ToStringExt();
  119. return new DateTime(strValue.Substring(0, 4).ToInt(), strValue.Substring(4, 2).ToInt(), strValue.Substring(6, 2).ToInt(),
  120. strValue.Substring(8, 2).ToInt(), strValue.Substring(10, 2).ToInt(), strValue.Substring(12, 2).ToInt());
  121. }
  122. DateTime result;
  123. bool isValid = DateTime.TryParse(data.ToString(), out result);
  124. if (isValid)
  125. return result;
  126. return null;
  127. }
  128. catch
  129. {
  130. return null;
  131. }
  132. }
  133. #endregion
  134. public static bool ToBool(this object data)
  135. {
  136. Boolean returnValue = false;
  137. if (data == null)
  138. return returnValue;
  139. else
  140. {
  141. if (Boolean.TryParse(data.ToString(), out returnValue))
  142. return returnValue;
  143. else
  144. return returnValue;
  145. }
  146. }
  147. public static Nullable<bool> ToBoolWithNull(this object data)
  148. {
  149. Boolean returnValue = false;
  150. if (data == null)
  151. return null;
  152. else
  153. {
  154. if (Boolean.TryParse(data.ToString(), out returnValue))
  155. return returnValue;
  156. else
  157. return null;
  158. }
  159. }
  160. #region 数字转换
  161. public static decimal ToDecimal(this object data)
  162. {
  163. decimal returnValue = 0;
  164. if (data == null)
  165. return returnValue;
  166. else
  167. {
  168. if (decimal.TryParse(data.ToString(), out returnValue))
  169. {
  170. return returnValue;
  171. }
  172. else
  173. {
  174. return 0;
  175. }
  176. }
  177. }
  178. public static Nullable<decimal> ToDecimalWithNull(this object data)
  179. {
  180. decimal returnValue = 0;
  181. if (data == null)
  182. return null;
  183. else
  184. {
  185. if (decimal.TryParse(data.ToString(), out returnValue))
  186. return returnValue;
  187. else
  188. return null;
  189. }
  190. }
  191. public static float ToFloat(this object data)
  192. {
  193. float returnValue = 0;
  194. if (data == null)
  195. return returnValue;
  196. else
  197. {
  198. if (float.TryParse(data.ToString(), out returnValue))
  199. {
  200. return returnValue;
  201. }
  202. else
  203. {
  204. return returnValue;
  205. }
  206. }
  207. }
  208. public static Nullable<float> ToFloatWithNull(this object data)
  209. {
  210. float returnValue = 0;
  211. if (data == null)
  212. return null;
  213. else
  214. {
  215. if (float.TryParse(data.ToString(), out returnValue))
  216. return returnValue;
  217. else
  218. return null;
  219. }
  220. }
  221. public static double ToDouble(this object data)
  222. {
  223. double returnValue = 0;
  224. if (data == null)
  225. return returnValue;
  226. else
  227. {
  228. if (double.TryParse(data.ToString(), out returnValue))
  229. {
  230. return returnValue;
  231. }
  232. else
  233. {
  234. return 0;
  235. }
  236. }
  237. }
  238. public static Nullable<double> ToDoubleWithNull(this object data)
  239. {
  240. double returnValue = 0;
  241. if (data == null)
  242. return null;
  243. else
  244. {
  245. if (double.TryParse(data.ToString(), out returnValue))
  246. return returnValue;
  247. else
  248. return null;
  249. }
  250. }
  251. public static Int64 ToInt64(this object data)
  252. {
  253. Int64 returnValue = 0;
  254. if (data == null)
  255. return returnValue;
  256. else
  257. {
  258. if (Int64.TryParse(data.ToString(), out returnValue))
  259. return returnValue;
  260. else
  261. return 0;
  262. }
  263. }
  264. public static Nullable<Int64> ToInt64WithNull(this object data)
  265. {
  266. Int64 returnValue = 0;
  267. if (data == null)
  268. return null;
  269. else
  270. {
  271. if (Int64.TryParse(data.ToString(), out returnValue))
  272. return returnValue;
  273. else
  274. return null;
  275. }
  276. }
  277. public static Int32 ToInt(this object data)
  278. {
  279. Int32 returnValue = 0;
  280. if (data == null)
  281. return returnValue;
  282. else
  283. {
  284. if (Int32.TryParse(data.ToString(), out returnValue))
  285. {
  286. return returnValue;
  287. }
  288. else
  289. {
  290. return 0;
  291. }
  292. }
  293. }
  294. public static Nullable<Int32> ToIntWithNull(this object data)
  295. {
  296. if (data == null)
  297. return null;
  298. int result;
  299. bool isValid = int.TryParse(data.ToString(), out result);
  300. if (isValid)
  301. return result;
  302. return null;
  303. }
  304. public static Int16 ToInt16(this object data)
  305. {
  306. Int16 returnValue = 0;
  307. if (data == null)
  308. return returnValue;
  309. else
  310. {
  311. if (Int16.TryParse(data.ToString(), out returnValue))
  312. {
  313. return returnValue;
  314. }
  315. else
  316. {
  317. return 0;
  318. }
  319. }
  320. }
  321. public static Nullable<Int16> ToInt16WithNull(this object data)
  322. {
  323. Int16 returnValue = 0;
  324. if (data == null)
  325. return null;
  326. else
  327. {
  328. if (Int16.TryParse(data.ToString(), out returnValue))
  329. return returnValue;
  330. else
  331. return null;
  332. }
  333. }
  334. public static bool IsNumeric(this object o)
  335. {
  336. if (o == null || o.ToString() == "")
  337. return false;
  338. string strNum = o.ToString();
  339. System.Text.RegularExpressions.Regex reg1
  340. = new System.Text.RegularExpressions.Regex(@"^-?[0-9]*$");
  341. return reg1.IsMatch(strNum);
  342. }
  343. #endregion
  344. }
  345. }