Extension.Convert.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace Extensions
  8. {
  9. public static partial class Extensions
  10. {
  11. #region 转换为long
  12. /// <summary>
  13. /// 将object转换为long,若转换失败,则返回0。不抛出异常。
  14. /// </summary>
  15. /// <param name="str"></param>
  16. /// <returns></returns>
  17. public static long ParseToLong(this object obj)
  18. {
  19. try
  20. {
  21. return long.Parse(obj.ToString());
  22. }
  23. catch
  24. {
  25. return 0L;
  26. }
  27. }
  28. /// <summary>
  29. /// 将object转换为long,若转换失败,则返回指定值。不抛出异常。
  30. /// </summary>
  31. /// <param name="str"></param>
  32. /// <param name="defaultValue"></param>
  33. /// <returns></returns>
  34. public static long ParseToLong(this string str, long defaultValue)
  35. {
  36. try
  37. {
  38. return long.Parse(str);
  39. }
  40. catch
  41. {
  42. return defaultValue;
  43. }
  44. }
  45. #endregion
  46. #region 转换为int
  47. /// <summary>
  48. /// 将object转换为int,若转换失败,则返回0。不抛出异常。
  49. /// </summary>
  50. /// <param name="str"></param>
  51. /// <returns></returns>
  52. public static int ParseToInt(this object str)
  53. {
  54. try
  55. {
  56. return Convert.ToInt32(str);
  57. }
  58. catch
  59. {
  60. return 0;
  61. }
  62. }
  63. /// <summary>
  64. /// 将object转换为int,若转换失败,则返回指定值。不抛出异常。
  65. /// null返回默认值
  66. /// </summary>
  67. /// <param name="str"></param>
  68. /// <param name="defaultValue"></param>
  69. /// <returns></returns>
  70. public static int ParseToInt(this object str, int defaultValue)
  71. {
  72. if (str == null)
  73. {
  74. return defaultValue;
  75. }
  76. try
  77. {
  78. return Convert.ToInt32(str);
  79. }
  80. catch
  81. {
  82. return defaultValue;
  83. }
  84. }
  85. #endregion
  86. #region 转换为short
  87. /// <summary>
  88. /// 将object转换为short,若转换失败,则返回0。不抛出异常。
  89. /// </summary>
  90. /// <param name="str"></param>
  91. /// <returns></returns>
  92. public static short ParseToShort(this object obj)
  93. {
  94. try
  95. {
  96. return short.Parse(obj.ToString());
  97. }
  98. catch
  99. {
  100. return 0;
  101. }
  102. }
  103. /// <summary>
  104. /// 将object转换为short,若转换失败,则返回指定值。不抛出异常。
  105. /// </summary>
  106. /// <param name="str"></param>
  107. /// <returns></returns>
  108. public static short ParseToShort(this object str, short defaultValue)
  109. {
  110. try
  111. {
  112. return short.Parse(str.ToString());
  113. }
  114. catch
  115. {
  116. return defaultValue;
  117. }
  118. }
  119. #endregion
  120. #region 转换为demical
  121. /// <summary>
  122. /// 将object转换为demical,若转换失败,则返回指定值。不抛出异常。
  123. /// </summary>
  124. /// <param name="str"></param>
  125. /// <returns></returns>
  126. public static decimal ParseToDecimal(this object str, decimal defaultValue)
  127. {
  128. try
  129. {
  130. return decimal.Parse(str.ToString());
  131. }
  132. catch
  133. {
  134. return defaultValue;
  135. }
  136. }
  137. /// <summary>
  138. /// 将object转换为demical,若转换失败,则返回0。不抛出异常。
  139. /// </summary>
  140. /// <param name="str"></param>
  141. /// <returns></returns>
  142. public static decimal ParseToDecimal(this object str)
  143. {
  144. try
  145. {
  146. return decimal.Parse(str.ToString());
  147. }
  148. catch
  149. {
  150. return 0;
  151. }
  152. }
  153. #endregion
  154. #region 转化为bool
  155. /// <summary>
  156. /// 将object转换为bool,若转换失败,则返回false。不抛出异常。
  157. /// </summary>
  158. /// <param name="str"></param>
  159. /// <returns></returns>
  160. public static bool ParseToBool(this object str)
  161. {
  162. try
  163. {
  164. return bool.Parse(str.ToString());
  165. }
  166. catch
  167. {
  168. return false;
  169. }
  170. }
  171. /// <summary>
  172. /// 将object转换为bool,若转换失败,则返回指定值。不抛出异常。
  173. /// </summary>
  174. /// <param name="str"></param>
  175. /// <returns></returns>
  176. public static bool ParseToBool(this object str, bool result)
  177. {
  178. try
  179. {
  180. return bool.Parse(str.ToString());
  181. }
  182. catch
  183. {
  184. return result;
  185. }
  186. }
  187. #endregion
  188. #region 转换为float
  189. /// <summary>
  190. /// 将object转换为float,若转换失败,则返回0。不抛出异常。
  191. /// </summary>
  192. /// <param name="str"></param>
  193. /// <returns></returns>
  194. public static float ParseToFloat(this object str)
  195. {
  196. try
  197. {
  198. return float.Parse(str.ToString());
  199. }
  200. catch
  201. {
  202. return 0;
  203. }
  204. }
  205. /// <summary>
  206. /// 将object转换为float,若转换失败,则返回指定值。不抛出异常。
  207. /// </summary>
  208. /// <param name="str"></param>
  209. /// <returns></returns>
  210. public static float ParseToFloat(this object str, float result)
  211. {
  212. try
  213. {
  214. return float.Parse(str.ToString());
  215. }
  216. catch
  217. {
  218. return result;
  219. }
  220. }
  221. #endregion
  222. #region 转换为Guid
  223. /// <summary>
  224. /// 将string转换为Guid,若转换失败,则返回Guid.Empty。不抛出异常。
  225. /// </summary>
  226. /// <param name="str"></param>
  227. /// <returns></returns>
  228. public static Guid ParseToGuid(this string str)
  229. {
  230. try
  231. {
  232. return new Guid(str);
  233. }
  234. catch
  235. {
  236. return Guid.Empty;
  237. }
  238. }
  239. #endregion
  240. #region 转换为DateTime
  241. /// <summary>
  242. /// 将string转换为DateTime,若转换失败,则返回日期最小值。不抛出异常。
  243. /// </summary>
  244. /// <param name="str"></param>
  245. /// <returns></returns>
  246. public static DateTime ParseToDateTime(this string str)
  247. {
  248. try
  249. {
  250. if (string.IsNullOrWhiteSpace(str))
  251. {
  252. return DateTime.MinValue;
  253. }
  254. if (str.Contains("-") || str.Contains("/"))
  255. {
  256. return DateTime.Parse(str);
  257. }
  258. else
  259. {
  260. int length = str.Length;
  261. switch (length)
  262. {
  263. case 4:
  264. return DateTime.ParseExact(str, "yyyy", System.Globalization.CultureInfo.CurrentCulture);
  265. case 6:
  266. return DateTime.ParseExact(str, "yyyyMM", System.Globalization.CultureInfo.CurrentCulture);
  267. case 8:
  268. return DateTime.ParseExact(str, "yyyyMMdd", System.Globalization.CultureInfo.CurrentCulture);
  269. case 10:
  270. return DateTime.ParseExact(str, "yyyyMMddHH", System.Globalization.CultureInfo.CurrentCulture);
  271. case 12:
  272. return DateTime.ParseExact(str, "yyyyMMddHHmm", System.Globalization.CultureInfo.CurrentCulture);
  273. case 14:
  274. return DateTime.ParseExact(str, "yyyyMMddHHmmss", System.Globalization.CultureInfo.CurrentCulture);
  275. default:
  276. return DateTime.ParseExact(str, "yyyyMMddHHmmss", System.Globalization.CultureInfo.CurrentCulture);
  277. }
  278. }
  279. }
  280. catch
  281. {
  282. return DateTime.MinValue;
  283. }
  284. }
  285. /// <summary>
  286. /// 将string转换为DateTime,若转换失败,则返回默认值。
  287. /// </summary>
  288. /// <param name="str"></param>
  289. /// <param name="defaultValue"></param>
  290. /// <returns></returns>
  291. public static DateTime ParseToDateTime(this string str, DateTime? defaultValue)
  292. {
  293. try
  294. {
  295. if (string.IsNullOrWhiteSpace(str))
  296. {
  297. return defaultValue.GetValueOrDefault();
  298. }
  299. if (str.Contains("-") || str.Contains("/"))
  300. {
  301. return DateTime.Parse(str);
  302. }
  303. else
  304. {
  305. int length = str.Length;
  306. switch (length)
  307. {
  308. case 4:
  309. return DateTime.ParseExact(str, "yyyy", System.Globalization.CultureInfo.CurrentCulture);
  310. case 6:
  311. return DateTime.ParseExact(str, "yyyyMM", System.Globalization.CultureInfo.CurrentCulture);
  312. case 8:
  313. return DateTime.ParseExact(str, "yyyyMMdd", System.Globalization.CultureInfo.CurrentCulture);
  314. case 10:
  315. return DateTime.ParseExact(str, "yyyyMMddHH", System.Globalization.CultureInfo.CurrentCulture);
  316. case 12:
  317. return DateTime.ParseExact(str, "yyyyMMddHHmm", System.Globalization.CultureInfo.CurrentCulture);
  318. case 14:
  319. return DateTime.ParseExact(str, "yyyyMMddHHmmss", System.Globalization.CultureInfo.CurrentCulture);
  320. default:
  321. return DateTime.ParseExact(str, "yyyyMMddHHmmss", System.Globalization.CultureInfo.CurrentCulture);
  322. }
  323. }
  324. }
  325. catch
  326. {
  327. return defaultValue.GetValueOrDefault();
  328. }
  329. }
  330. #endregion
  331. #region 转换为string
  332. /// <summary>
  333. /// 将object转换为string,若转换失败,则返回""。不抛出异常。
  334. /// </summary>
  335. /// <param name="str"></param>
  336. /// <returns></returns>
  337. public static string ParseToString(this object obj)
  338. {
  339. try
  340. {
  341. if (obj == null)
  342. {
  343. return string.Empty;
  344. }
  345. else
  346. {
  347. return obj.ToString();
  348. }
  349. }
  350. catch
  351. {
  352. return string.Empty;
  353. }
  354. }
  355. public static string ParseToStrings<T>(this object obj)
  356. {
  357. try
  358. {
  359. var list = obj as IEnumerable<T>;
  360. if (list != null)
  361. {
  362. return string.Join(",", list);
  363. }
  364. else
  365. {
  366. return obj.ToString();
  367. }
  368. }
  369. catch
  370. {
  371. return string.Empty;
  372. }
  373. }
  374. #endregion
  375. #region 转换为double
  376. /// <summary>
  377. /// 将object转换为double,若转换失败,则返回0。不抛出异常。
  378. /// </summary>
  379. /// <param name="obj"></param>
  380. /// <returns></returns>
  381. public static double ParseToDouble(this object obj)
  382. {
  383. try
  384. {
  385. return double.Parse(obj.ToString());
  386. }
  387. catch
  388. {
  389. return 0;
  390. }
  391. }
  392. /// <summary>
  393. /// 将object转换为double,若转换失败,则返回指定值。不抛出异常。
  394. /// </summary>
  395. /// <param name="str"></param>
  396. /// <param name="defaultValue"></param>
  397. /// <returns></returns>
  398. public static double ParseToDouble(this object str, double defaultValue)
  399. {
  400. try
  401. {
  402. return double.Parse(str.ToString());
  403. }
  404. catch
  405. {
  406. return defaultValue;
  407. }
  408. }
  409. #endregion
  410. #region 强制转换类型
  411. /// <summary>
  412. /// 强制转换类型
  413. /// </summary>
  414. /// <typeparam name="TResult"></typeparam>
  415. /// <param name="source"></param>
  416. /// <returns></returns>
  417. public static IEnumerable<TResult> CastSuper<TResult>(this IEnumerable source)
  418. {
  419. foreach (object item in source)
  420. {
  421. yield return (TResult)Convert.ChangeType(item, typeof(TResult));
  422. }
  423. }
  424. #endregion
  425. }
  426. }