CNAS取数仪器端升级
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.

ConvertUtilType.cs 15KB

4 months ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace CnasSynchronousCommon
  6. {
  7. public class ConvertUtilType
  8. {
  9. #region ToInt32
  10. public static int? ToInt32(string obj)
  11. {
  12. return ToInt32(obj, null);
  13. }
  14. public static int? ToInt32(object obj)
  15. {
  16. return ToInt32(obj, null);
  17. }
  18. public static int? ToInt32(object obj, int? defaultValue)
  19. {
  20. if (obj == DBNull.Value || obj == null)
  21. {
  22. return defaultValue;
  23. }
  24. return ToInt32(Convert.ToString(obj), defaultValue);
  25. }
  26. public static int? ToInt32(string obj, int? defaultValue)
  27. {
  28. if (string.IsNullOrEmpty(obj))
  29. {
  30. return defaultValue;
  31. }
  32. int outValue;
  33. if (int.TryParse(obj, out outValue))
  34. {
  35. return outValue;
  36. }
  37. else
  38. {
  39. return defaultValue;
  40. }
  41. }
  42. #endregion
  43. #region ToInt16
  44. public static short? ToInt16(string obj)
  45. {
  46. return ToInt16(obj, null);
  47. }
  48. public static short? ToInt16(object obj)
  49. {
  50. return ToInt16(obj, null);
  51. }
  52. public static short? ToInt16(object obj, short? defaultValue)
  53. {
  54. if (obj == DBNull.Value || obj == null)
  55. {
  56. return defaultValue;
  57. }
  58. return ToInt16(Convert.ToString(obj), defaultValue);
  59. }
  60. public static short? ToInt16(string obj, short? defaultValue)
  61. {
  62. if (string.IsNullOrEmpty(obj))
  63. {
  64. return defaultValue;
  65. }
  66. short outValue;
  67. if (short.TryParse(obj, out outValue))
  68. {
  69. return outValue;
  70. }
  71. else
  72. {
  73. return defaultValue;
  74. }
  75. }
  76. #endregion
  77. #region ToInt64
  78. public static long? ToInt64(string obj)
  79. {
  80. return ToInt64(obj, null);
  81. }
  82. public static long? ToInt64(object obj)
  83. {
  84. return ToInt64(obj, null);
  85. }
  86. public static long? ToInt64(object obj, long? defaultValue)
  87. {
  88. if (obj == DBNull.Value || obj == null)
  89. {
  90. return defaultValue;
  91. }
  92. return ToInt64(Convert.ToString(obj), defaultValue);
  93. }
  94. public static long? ToInt64(string obj, long? defaultValue)
  95. {
  96. if (string.IsNullOrEmpty(obj))
  97. {
  98. return defaultValue;
  99. }
  100. long outValue;
  101. if (long.TryParse(obj, out outValue))
  102. {
  103. return outValue;
  104. }
  105. else
  106. {
  107. return defaultValue;
  108. }
  109. }
  110. #endregion
  111. #region ToUInt32
  112. public static uint? ToUInt32(string obj)
  113. {
  114. return ToUInt32(obj, null);
  115. }
  116. public static uint? ToUInt32(object obj)
  117. {
  118. return ToUInt32(obj, null);
  119. }
  120. public static uint? ToUInt32(object obj, uint? defaultValue)
  121. {
  122. if (obj == DBNull.Value || obj == null)
  123. {
  124. return defaultValue;
  125. }
  126. return ToUInt32(Convert.ToString(obj), defaultValue);
  127. }
  128. public static uint? ToUInt32(string obj, uint? defaultValue)
  129. {
  130. if (string.IsNullOrEmpty(obj))
  131. {
  132. return defaultValue;
  133. }
  134. uint outValue;
  135. if (uint.TryParse(obj, out outValue))
  136. {
  137. return outValue;
  138. }
  139. else
  140. {
  141. return defaultValue;
  142. }
  143. }
  144. #endregion
  145. #region ToUInt16
  146. public static ushort? ToUInt16(string obj)
  147. {
  148. return ToUInt16(obj, null);
  149. }
  150. public static ushort? ToUInt16(object obj)
  151. {
  152. return ToUInt16(obj, null);
  153. }
  154. public static ushort? ToUInt16(object obj, ushort? defaultValue)
  155. {
  156. if (obj == DBNull.Value || obj == null)
  157. {
  158. return defaultValue;
  159. }
  160. return ToUInt16(Convert.ToString(obj), defaultValue);
  161. }
  162. public static ushort? ToUInt16(string obj, ushort? defaultValue)
  163. {
  164. if (string.IsNullOrEmpty(obj))
  165. {
  166. return defaultValue;
  167. }
  168. ushort outValue;
  169. if (ushort.TryParse(obj, out outValue))
  170. {
  171. return outValue;
  172. }
  173. else
  174. {
  175. return defaultValue;
  176. }
  177. }
  178. #endregion
  179. #region ToUInt64
  180. public static ulong? ToUInt64(string obj)
  181. {
  182. return ToUInt64(obj, null);
  183. }
  184. public static ulong? ToUInt64(object obj)
  185. {
  186. return ToUInt64(obj, null);
  187. }
  188. public static ulong? ToUInt64(object obj, ulong? defaultValue)
  189. {
  190. if (obj == DBNull.Value || obj == null)
  191. {
  192. return defaultValue;
  193. }
  194. return ToUInt64(Convert.ToString(obj), defaultValue);
  195. }
  196. public static ulong? ToUInt64(string obj, ulong? defaultValue)
  197. {
  198. if (string.IsNullOrEmpty(obj))
  199. {
  200. return defaultValue;
  201. }
  202. ulong outValue;
  203. if (ulong.TryParse(obj, out outValue))
  204. {
  205. return outValue;
  206. }
  207. else
  208. {
  209. return defaultValue;
  210. }
  211. }
  212. #endregion
  213. #region ToDateTime
  214. public static DateTime? ToDateTime(string obj)
  215. {
  216. return ToDateTime(obj, null);
  217. }
  218. public static DateTime? ToDateTime(object obj)
  219. {
  220. return ToDateTime(obj, null);
  221. }
  222. public static DateTime? ToDateTime(object obj, DateTime? defaultValue)
  223. {
  224. if (obj == DBNull.Value || obj == null || obj.Equals(""))
  225. {
  226. return defaultValue;
  227. }
  228. return Convert.ToDateTime(Convert.ToDateTime(obj).ToString("yyyy-MM-dd HH:mm"));
  229. //return ToDateTime(Convert.ToString(obj), defaultValue);
  230. }
  231. public static DateTime? ToDateTime(string obj, DateTime? defaultValue)
  232. {
  233. if (string.IsNullOrEmpty(obj))
  234. {
  235. return defaultValue;
  236. }
  237. DateTime outValue;
  238. if (DateTime.TryParse(obj, out outValue))
  239. {
  240. return outValue;
  241. }
  242. else
  243. {
  244. return defaultValue;
  245. }
  246. }
  247. #endregion
  248. #region ToBoolean
  249. public static bool? ToBoolean(string obj)
  250. {
  251. return ToBoolean(obj, null);
  252. }
  253. public static bool? ToBoolean(object obj)
  254. {
  255. return ToBoolean(obj, null);
  256. }
  257. public static bool? ToBoolean(object obj, bool? defaultValue)
  258. {
  259. if (obj == DBNull.Value || obj == null)
  260. {
  261. return defaultValue;
  262. }
  263. return ToBoolean(Convert.ToString(obj), defaultValue);
  264. }
  265. public static bool? ToBoolean(string obj, bool? defaultValue)
  266. {
  267. if (!string.IsNullOrEmpty(obj))
  268. {
  269. bool flag;
  270. if (bool.TryParse(obj, out flag))
  271. {
  272. return new bool?(flag);
  273. }
  274. if (obj == "1")
  275. {
  276. return true;
  277. }
  278. if (obj == "0")
  279. {
  280. return false;
  281. }
  282. }
  283. return defaultValue;
  284. }
  285. #endregion
  286. #region ToString
  287. public static string ToString(object obj)
  288. {
  289. return ToString(obj, null);
  290. }
  291. public static string ToString(object obj, string defaultValue)
  292. {
  293. if (obj == DBNull.Value || obj == null)
  294. {
  295. return defaultValue;
  296. }
  297. return System.Convert.ToString(obj);
  298. }
  299. public static string ToDateString(DateTime? obj, string defaultValue)
  300. {
  301. if (obj == null)
  302. {
  303. return defaultValue;
  304. }
  305. return obj.Value.ToString("yyyy-MM-dd");
  306. }
  307. public static string ToDateString(object obj, string defaultValue)
  308. {
  309. if (obj == DBNull.Value || obj == null)
  310. {
  311. return defaultValue;
  312. }
  313. DateTime? dateTime = ToDateTime(obj);
  314. return ToDateString(dateTime, defaultValue);
  315. }
  316. #endregion
  317. #region ToByte
  318. public static byte? ToByte(string obj)
  319. {
  320. return ToByte(obj, null);
  321. }
  322. public static byte? ToByte(object obj)
  323. {
  324. return ToByte(obj, null);
  325. }
  326. public static byte? ToByte(object obj, byte? defaultValue)
  327. {
  328. if (obj == DBNull.Value || obj == null)
  329. {
  330. return defaultValue;
  331. }
  332. return ToByte(Convert.ToString(obj), defaultValue);
  333. }
  334. public static byte? ToByte(string obj, byte? defaultValue)
  335. {
  336. if (string.IsNullOrEmpty(obj))
  337. {
  338. return defaultValue;
  339. }
  340. byte outValue;
  341. if (byte.TryParse(obj, out outValue))
  342. {
  343. return outValue;
  344. }
  345. else
  346. {
  347. return defaultValue;
  348. }
  349. }
  350. #endregion
  351. #region ToSByte
  352. public static sbyte? ToSByte(string obj)
  353. {
  354. return ToSByte(obj, null);
  355. }
  356. public static sbyte? ToSByte(object obj)
  357. {
  358. return ToSByte(obj, null);
  359. }
  360. public static sbyte? ToSByte(object obj, sbyte? defaultValue)
  361. {
  362. if (obj == DBNull.Value || obj == null)
  363. {
  364. return defaultValue;
  365. }
  366. return ToSByte(Convert.ToString(obj), defaultValue);
  367. }
  368. public static sbyte? ToSByte(string obj, sbyte? defaultValue)
  369. {
  370. if (string.IsNullOrEmpty(obj))
  371. {
  372. return defaultValue;
  373. }
  374. sbyte outValue;
  375. if (sbyte.TryParse(obj, out outValue))
  376. {
  377. return outValue;
  378. }
  379. else
  380. {
  381. return defaultValue;
  382. }
  383. }
  384. #endregion
  385. #region ToDouble
  386. public static double? ToDouble(string obj)
  387. {
  388. return ToDouble(obj, null);
  389. }
  390. public static double? ToDouble(object obj)
  391. {
  392. return ToDouble(obj, null);
  393. }
  394. public static double? ToDouble(object obj, double? defaultValue)
  395. {
  396. if (obj == DBNull.Value || obj == null)
  397. {
  398. return defaultValue;
  399. }
  400. return ToDouble(Convert.ToString(obj), defaultValue);
  401. }
  402. public static double? ToDouble(string obj, double? defaultValue)
  403. {
  404. if (string.IsNullOrEmpty(obj))
  405. {
  406. return defaultValue;
  407. }
  408. double outValue;
  409. if (double.TryParse(obj, out outValue))
  410. {
  411. return outValue;
  412. }
  413. else
  414. {
  415. return defaultValue;
  416. }
  417. }
  418. #endregion
  419. #region ToSingle
  420. public static Single? ToSingle(string obj)
  421. {
  422. return ToSingle(obj, null);
  423. }
  424. public static Single? ToSingle(object obj)
  425. {
  426. return ToSingle(obj, null);
  427. }
  428. public static Single? ToSingle(object obj, Single? defaultValue)
  429. {
  430. if (obj == DBNull.Value || obj == null)
  431. {
  432. return defaultValue;
  433. }
  434. return ToSingle(Convert.ToString(obj), defaultValue);
  435. }
  436. public static Single? ToSingle(string obj, Single? defaultValue)
  437. {
  438. if (string.IsNullOrEmpty(obj))
  439. {
  440. return defaultValue;
  441. }
  442. Single outValue;
  443. if (Single.TryParse(obj, out outValue))
  444. {
  445. return outValue;
  446. }
  447. else
  448. {
  449. return defaultValue;
  450. }
  451. }
  452. #endregion
  453. #region ToChar
  454. public static char? ToChar(string obj)
  455. {
  456. return ToChar(obj, null);
  457. }
  458. public static char? ToChar(object obj)
  459. {
  460. return ToChar(obj, null);
  461. }
  462. public static char? ToChar(object obj, char? defaultValue)
  463. {
  464. if (obj == DBNull.Value || obj == null)
  465. {
  466. return defaultValue;
  467. }
  468. return ToChar(Convert.ToString(obj), defaultValue);
  469. }
  470. public static char? ToChar(string obj, char? defaultValue)
  471. {
  472. if (string.IsNullOrEmpty(obj))
  473. {
  474. return defaultValue;
  475. }
  476. char outValue;
  477. if (char.TryParse(obj, out outValue))
  478. {
  479. return outValue;
  480. }
  481. else
  482. {
  483. return defaultValue;
  484. }
  485. }
  486. #endregion
  487. #region ToDecimal
  488. public static decimal? ToDecimal(string obj)
  489. {
  490. return ToDecimal(obj, null);
  491. }
  492. public static decimal? ToDecimal(object obj)
  493. {
  494. return ToDecimal(obj, null);
  495. }
  496. public static decimal? ToDecimal(object obj, decimal? defaultValue)
  497. {
  498. if (obj == DBNull.Value || obj == null)
  499. {
  500. return defaultValue;
  501. }
  502. return ToDecimal(Convert.ToString(obj), defaultValue);
  503. }
  504. public static decimal? ToDecimal(string obj, decimal? defaultValue)
  505. {
  506. if (string.IsNullOrEmpty(obj))
  507. {
  508. return defaultValue;
  509. }
  510. decimal outValue;
  511. if (decimal.TryParse(obj, out outValue))
  512. {
  513. return outValue;
  514. }
  515. else
  516. {
  517. return defaultValue;
  518. }
  519. }
  520. #endregion
  521. }
  522. }