Ext.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. // ***********************************************************************
  2. // Assembly : HZH_Controls
  3. // Created : 08-08-2019
  4. //
  5. // ***********************************************************************
  6. // <copyright file="Ext.cs">
  7. // Copyright by Huang Zhenghui(黄正辉) All, QQ group:568015492 QQ:623128629 Email:623128629@qq.com
  8. // </copyright>
  9. //
  10. // Blog: https://www.cnblogs.com/bfyx
  11. // GitHub:https://github.com/kwwwvagaa/NetWinformControl
  12. // gitee:https://gitee.com/kwwwvagaa/net_winform_custom_control.git
  13. //
  14. // If you use this code, please keep this note.
  15. // ***********************************************************************
  16. using System;
  17. using System.Collections.Generic;
  18. using System.Linq;
  19. using System.Reflection;
  20. using System.Text;
  21. namespace HZH_Controls
  22. {
  23. /// <summary>
  24. /// Class Ext.
  25. /// </summary>
  26. public static partial class Ext
  27. {
  28. /// <summary>
  29. /// Clones the model.
  30. /// </summary>
  31. /// <typeparam name="T"></typeparam>
  32. /// <param name="classObject">The class object.</param>
  33. /// <returns>T.</returns>
  34. public static T CloneModel<T>(this T classObject) where T : class
  35. {
  36. T result;
  37. if (classObject == null)
  38. {
  39. result = default(T);
  40. }
  41. else
  42. {
  43. object obj = Activator.CreateInstance(typeof(T));
  44. PropertyInfo[] properties = typeof(T).GetProperties();
  45. PropertyInfo[] array = properties;
  46. for (int i = 0; i < array.Length; i++)
  47. {
  48. PropertyInfo propertyInfo = array[i];
  49. if (propertyInfo.CanWrite)
  50. propertyInfo.SetValue(obj, propertyInfo.GetValue(classObject, null), null);
  51. }
  52. result = (obj as T);
  53. }
  54. return result;
  55. }
  56. /// <summary>
  57. /// ASCII编码的数组转换为英文字符串
  58. /// </summary>
  59. /// <param name="s">字符串</param>
  60. /// <returns>结果</returns>
  61. public static string ToEnString(this byte[] s)
  62. {
  63. return ToEncodeString(s, Encoding.ASCII).Trim('\0').Trim();
  64. }
  65. /// <summary>
  66. /// 数组按指定编码转换为字符串
  67. /// </summary>
  68. /// <param name="dealBytes">数组</param>
  69. /// <param name="encode">编码</param>
  70. /// <returns>结果</returns>
  71. public static string ToEncodeString(this byte[] dealBytes, Encoding encode)
  72. {
  73. return encode.GetString(dealBytes);
  74. }
  75. #region 转换为base64字符串
  76. /// <summary>
  77. /// 功能描述:转换为base64字符串
  78. /// 作  者:HZH
  79. /// 创建日期:2019-03-29 10:12:38
  80. /// 任务编号:POS
  81. /// </summary>
  82. /// <param name="data">data</param>
  83. /// <returns>返回值</returns>
  84. public static string ToBase64Str(this string data)
  85. {
  86. if (data.IsEmpty())
  87. return string.Empty;
  88. byte[] buffer = Encoding.Default.GetBytes(data);
  89. return Convert.ToBase64String(buffer);
  90. }
  91. #endregion
  92. /// <summary>
  93. /// 转换为坐标
  94. /// </summary>
  95. /// <param name="data">The data.</param>
  96. /// <returns>System.Drawing.Point.</returns>
  97. public static System.Drawing.Point ToPoint(this string data)
  98. {
  99. if (!System.Text.RegularExpressions.Regex.IsMatch(data, @"^\s*\d+(\.\d+)?\s*\,\s*\d+(\.\d+)?\s*$"))
  100. {
  101. return System.Drawing.Point.Empty;
  102. }
  103. else
  104. {
  105. string[] strs = data.Split(',');
  106. return new System.Drawing.Point(strs[0].ToInt(), strs[1].ToInt());
  107. }
  108. }
  109. #region 数值转换
  110. /// <summary>
  111. /// 转换为整型
  112. /// </summary>
  113. /// <param name="data">数据</param>
  114. /// <returns>System.Int32.</returns>
  115. public static int ToInt(this object data)
  116. {
  117. if (data == null)
  118. return 0;
  119. if (data is bool)
  120. {
  121. return (bool)data ? 1 : 0;
  122. }
  123. int result;
  124. var success = int.TryParse(data.ToString(), out result);
  125. if (success)
  126. return result;
  127. try
  128. {
  129. return Convert.ToInt32(ToDouble(data, 0));
  130. }
  131. catch (Exception)
  132. {
  133. return 0;
  134. }
  135. }
  136. /// <summary>
  137. /// 转换为可空整型
  138. /// </summary>
  139. /// <param name="data">数据</param>
  140. /// <returns>System.Nullable&lt;System.Int32&gt;.</returns>
  141. public static int? ToIntOrNull(this object data)
  142. {
  143. if (data == null)
  144. return null;
  145. int result;
  146. bool isValid = int.TryParse(data.ToString(), out result);
  147. if (isValid)
  148. return result;
  149. return null;
  150. }
  151. /// <summary>
  152. /// 转换为双精度浮点数
  153. /// </summary>
  154. /// <param name="data">数据</param>
  155. /// <returns>System.Double.</returns>
  156. public static double ToDouble(this object data)
  157. {
  158. if (data == null)
  159. return 0;
  160. double result;
  161. return double.TryParse(data.ToString(), out result) ? result : 0;
  162. }
  163. /// <summary>
  164. /// 转换为双精度浮点数,并按指定的小数位4舍5入
  165. /// </summary>
  166. /// <param name="data">数据</param>
  167. /// <param name="digits">小数位数</param>
  168. /// <returns>System.Double.</returns>
  169. public static double ToDouble(this object data, int digits)
  170. {
  171. return Math.Round(ToDouble(data), digits, System.MidpointRounding.AwayFromZero);
  172. }
  173. /// <summary>
  174. /// 转换为可空双精度浮点数
  175. /// </summary>
  176. /// <param name="data">数据</param>
  177. /// <returns>System.Nullable&lt;System.Double&gt;.</returns>
  178. public static double? ToDoubleOrNull(this object data)
  179. {
  180. if (data == null)
  181. return null;
  182. double result;
  183. bool isValid = double.TryParse(data.ToString(), out result);
  184. if (isValid)
  185. return result;
  186. return null;
  187. }
  188. /// <summary>
  189. /// 转换为高精度浮点数
  190. /// </summary>
  191. /// <param name="data">数据</param>
  192. /// <returns>System.Decimal.</returns>
  193. public static decimal ToDecimal(this object data)
  194. {
  195. if (data == null)
  196. return 0;
  197. decimal result;
  198. return decimal.TryParse(data.ToString(), out result) ? result : 0;
  199. }
  200. /// <summary>
  201. /// 转换为高精度浮点数,并按指定的小数位4舍5入
  202. /// </summary>
  203. /// <param name="data">数据</param>
  204. /// <param name="digits">小数位数</param>
  205. /// <returns>System.Decimal.</returns>
  206. public static decimal ToDecimal(this object data, int digits)
  207. {
  208. return Math.Round(ToDecimal(data), digits, System.MidpointRounding.AwayFromZero);
  209. }
  210. /// <summary>
  211. /// 转换为可空高精度浮点数
  212. /// </summary>
  213. /// <param name="data">数据</param>
  214. /// <returns>System.Nullable&lt;System.Decimal&gt;.</returns>
  215. public static decimal? ToDecimalOrNull(this object data)
  216. {
  217. if (data == null)
  218. return null;
  219. decimal result;
  220. bool isValid = decimal.TryParse(data.ToString(), out result);
  221. if (isValid)
  222. return result;
  223. return null;
  224. }
  225. /// <summary>
  226. /// 转换为可空高精度浮点数,并按指定的小数位4舍5入
  227. /// </summary>
  228. /// <param name="data">数据</param>
  229. /// <param name="digits">小数位数</param>
  230. /// <returns>System.Nullable&lt;System.Decimal&gt;.</returns>
  231. public static decimal? ToDecimalOrNull(this object data, int digits)
  232. {
  233. var result = ToDecimalOrNull(data);
  234. if (result == null)
  235. return null;
  236. return Math.Round(result.Value, digits, System.MidpointRounding.AwayFromZero);
  237. }
  238. #endregion
  239. #region 日期转换
  240. /// <summary>
  241. /// 转换为日期
  242. /// </summary>
  243. /// <param name="data">数据</param>
  244. /// <returns>DateTime.</returns>
  245. public static DateTime ToDate(this object data)
  246. {
  247. try
  248. {
  249. if (data == null)
  250. return DateTime.MinValue;
  251. if (System.Text.RegularExpressions.Regex.IsMatch(data.ToStringExt(), @"^\d{8}$"))
  252. {
  253. string strValue = data.ToStringExt();
  254. return new DateTime(strValue.Substring(0, 4).ToInt(), strValue.Substring(4, 2).ToInt(), strValue.Substring(6, 2).ToInt());
  255. }
  256. DateTime result;
  257. return DateTime.TryParse(data.ToString(), out result) ? result : DateTime.MinValue;
  258. }
  259. catch
  260. {
  261. return DateTime.MinValue;
  262. }
  263. }
  264. /// <summary>
  265. /// 转换为可空日期
  266. /// </summary>
  267. /// <param name="data">数据</param>
  268. /// <returns>System.Nullable&lt;DateTime&gt;.</returns>
  269. public static DateTime? ToDateOrNull(this object data)
  270. {
  271. try
  272. {
  273. if (data == null)
  274. return null;
  275. if (System.Text.RegularExpressions.Regex.IsMatch(data.ToStringExt(), @"^\d{8}$"))
  276. {
  277. string strValue = data.ToStringExt();
  278. return new DateTime(strValue.Substring(0, 4).ToInt(), strValue.Substring(4, 2).ToInt(), strValue.Substring(6, 2).ToInt());
  279. }
  280. DateTime result;
  281. bool isValid = DateTime.TryParse(data.ToString(), out result);
  282. if (isValid)
  283. return result;
  284. return null;
  285. }
  286. catch
  287. {
  288. return null;
  289. }
  290. }
  291. #endregion
  292. #region 布尔转换
  293. /// <summary>
  294. /// 转换为布尔值
  295. /// </summary>
  296. /// <param name="data">数据</param>
  297. /// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns>
  298. public static bool ToBool(this object data)
  299. {
  300. if (data == null)
  301. return false;
  302. bool? value = GetBool(data);
  303. if (value != null)
  304. return value.Value;
  305. bool result;
  306. return bool.TryParse(data.ToString(), out result) && result;
  307. }
  308. /// <summary>
  309. /// 获取布尔值
  310. /// </summary>
  311. /// <param name="data">The data.</param>
  312. /// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns>
  313. private static bool? GetBool(this object data)
  314. {
  315. switch (data.ToString().Trim().ToLower())
  316. {
  317. case "0":
  318. return false;
  319. case "1":
  320. return true;
  321. case "是":
  322. return true;
  323. case "否":
  324. return false;
  325. case "yes":
  326. return true;
  327. case "no":
  328. return false;
  329. default:
  330. return null;
  331. }
  332. }
  333. /// <summary>
  334. /// 转换为可空布尔值
  335. /// </summary>
  336. /// <param name="data">数据</param>
  337. /// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns>
  338. public static bool? ToBoolOrNull(this object data)
  339. {
  340. if (data == null)
  341. return null;
  342. bool? value = GetBool(data);
  343. if (value != null)
  344. return value.Value;
  345. bool result;
  346. bool isValid = bool.TryParse(data.ToString(), out result);
  347. if (isValid)
  348. return result;
  349. return null;
  350. }
  351. #endregion
  352. #region 字符串转换
  353. /// <summary>
  354. /// 字符串转换为byte[]
  355. /// </summary>
  356. /// <param name="data">The data.</param>
  357. /// <returns>System.Byte[].</returns>
  358. public static byte[] ToBytes(this string data)
  359. {
  360. return System.Text.Encoding.GetEncoding("GBK").GetBytes(data);
  361. }
  362. /// <summary>
  363. /// Converts to bytesdefault.
  364. /// </summary>
  365. /// <param name="data">The data.</param>
  366. /// <returns>System.Byte[].</returns>
  367. public static byte[] ToBytesDefault(this string data)
  368. {
  369. return System.Text.Encoding.Default.GetBytes(data);
  370. }
  371. /// <summary>
  372. /// 转换为字符串
  373. /// </summary>
  374. /// <param name="data">数据</param>
  375. /// <returns>System.String.</returns>
  376. public static string ToStringExt(this object data)
  377. {
  378. return data == null ? string.Empty : data.ToString();
  379. }
  380. #endregion
  381. /// <summary>
  382. /// 安全返回值
  383. /// </summary>
  384. /// <typeparam name="T"></typeparam>
  385. /// <param name="value">可空值</param>
  386. /// <returns>T.</returns>
  387. public static T SafeValue<T>(this T? value) where T : struct
  388. {
  389. return value ?? default(T);
  390. }
  391. /// <summary>
  392. /// 是否为空
  393. /// </summary>
  394. /// <param name="value">值</param>
  395. /// <returns><c>true</c> if the specified value is empty; otherwise, <c>false</c>.</returns>
  396. public static bool IsEmpty(this string value)
  397. {
  398. return string.IsNullOrWhiteSpace(value);
  399. }
  400. /// <summary>
  401. /// 是否为空
  402. /// </summary>
  403. /// <param name="value">值</param>
  404. /// <returns><c>true</c> if the specified value is empty; otherwise, <c>false</c>.</returns>
  405. public static bool IsEmpty(this Guid? value)
  406. {
  407. if (value == null)
  408. return true;
  409. return IsEmpty(value.Value);
  410. }
  411. /// <summary>
  412. /// 是否为空
  413. /// </summary>
  414. /// <param name="value">值</param>
  415. /// <returns><c>true</c> if the specified value is empty; otherwise, <c>false</c>.</returns>
  416. public static bool IsEmpty(this Guid value)
  417. {
  418. if (value == Guid.Empty)
  419. return true;
  420. return false;
  421. }
  422. /// <summary>
  423. /// 是否为空
  424. /// </summary>
  425. /// <param name="value">值</param>
  426. /// <returns><c>true</c> if the specified value is empty; otherwise, <c>false</c>.</returns>
  427. public static bool IsEmpty(this object value)
  428. {
  429. if (value != null && !string.IsNullOrEmpty(value.ToString()))
  430. {
  431. return false;
  432. }
  433. else
  434. {
  435. return true;
  436. }
  437. }
  438. #region 是否数字
  439. /// <summary>
  440. /// 功能描述:是否数字
  441. /// 作  者:HZH
  442. /// 创建日期:2019-03-06 09:03:05
  443. /// 任务编号:POS
  444. /// </summary>
  445. /// <param name="value">value</param>
  446. /// <returns>返回值</returns>
  447. public static bool IsNum(this string value)
  448. {
  449. return System.Text.RegularExpressions.Regex.IsMatch(value, @"^\d+(\.\d*)?$");
  450. }
  451. #endregion
  452. }
  453. }