ConvertExtension.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Reflection;
  7. namespace Common
  8. {
  9. /// <summary>
  10. /// 数据类型转换扩展方法
  11. /// </summary>
  12. public static class ConvertExtension
  13. {
  14. #region 类型转换
  15. /// <summary>
  16. /// object 转换成string 包括为空的情况
  17. /// </summary>
  18. /// <param name="obj"></param>
  19. /// <returns>返回值不含空格</returns>
  20. public static string ToStringEx(this object obj)
  21. {
  22. return obj == null ? string.Empty : obj.ToString().Trim();
  23. }
  24. /// <summary>
  25. /// 时间object 转换成格式化的string 包括为空的情况
  26. /// </summary>
  27. /// <param name="obj"></param>
  28. /// <param name="format"></param>
  29. /// <returns>返回值不含空格</returns>
  30. public static string TryToDateTimeToString(this object obj, string format)
  31. {
  32. if (obj == null)
  33. return string.Empty;
  34. DateTime dt;
  35. if (DateTime.TryParse(obj.ToString(), out dt))
  36. return dt.ToString(format);
  37. else
  38. return string.Empty;
  39. }
  40. /// <summary>
  41. /// 字符转Int
  42. /// </summary>
  43. /// <param name="obj"></param>
  44. /// <returns>成功:返回对应Int值;失败:返回0</returns>
  45. public static int TryToInt32(this object obj)
  46. {
  47. int rel = 0;
  48. if (!string.IsNullOrEmpty(obj.ToStringEx()))
  49. {
  50. int.TryParse(obj.ToStringEx(), out rel);
  51. }
  52. return rel;
  53. }
  54. /// <summary>
  55. /// 字符转Int64
  56. /// </summary>
  57. /// <param name="obj"></param>
  58. /// <returns></returns>
  59. public static Int64 TryToInt64(this object obj)
  60. {
  61. Int64 rel = 0;
  62. if (!string.IsNullOrEmpty(obj.ToStringEx()))
  63. {
  64. Int64.TryParse(obj.ToStringEx(), out rel);
  65. }
  66. return rel;
  67. }
  68. /// <summary>
  69. /// 字符转DateTime
  70. /// </summary>
  71. /// <param name="obj"></param>
  72. /// <returns>成功:返回对应Int值;失败:时间初始值</returns>
  73. public static DateTime TryToDateTime(this object obj)
  74. {
  75. DateTime rel = new DateTime();
  76. if (!string.IsNullOrEmpty(obj.ToStringEx()))
  77. {
  78. DateTime.TryParse(obj.ToStringEx(), out rel);
  79. }
  80. return rel;
  81. }
  82. /// <summary>
  83. /// 转换成bool类型
  84. /// </summary>
  85. /// <param name="obj"></param>
  86. /// <returns></returns>
  87. public static Boolean TryToBoolean(this object obj)
  88. {
  89. Boolean rel = false;
  90. if (!string.IsNullOrEmpty(obj.ToStringEx()))
  91. {
  92. string s = obj.ToStringEx();
  93. if (s.Equals("true") || s.Equals("1") || s.Equals("是"))
  94. {
  95. rel = true;
  96. }
  97. else
  98. {
  99. Boolean.TryParse(obj.ToStringEx(), out rel);
  100. }
  101. }
  102. return rel;
  103. }
  104. #endregion
  105. #region 补足位数
  106. /// <summary>
  107. /// 指定字符串的固定长度,如果字符串小于固定长度,
  108. /// 则在字符串的前面补足零,可设置的固定长度最大为9位
  109. /// </summary>
  110. /// <param name="text">原始字符串</param>
  111. /// <param name="limitedLength">字符串的固定长度</param>
  112. public static string RepairZero(this string text, int limitedLength)
  113. {
  114. //补足0的字符串
  115. string temp = "";
  116. //补足0
  117. for (int i = 0; i < limitedLength - text.Length; i++)
  118. {
  119. temp += "0";
  120. }
  121. //连接text
  122. temp += text;
  123. //返回补足0的字符串
  124. return temp;
  125. }
  126. /// <summary>
  127. /// 小时、分钟、秒小于10补足0
  128. /// </summary>
  129. /// <param name="text">原始字符串</param>
  130. /// <returns></returns>
  131. public static string RepairZero(this int text)
  132. {
  133. string res = string.Empty;
  134. if (text >= 0 && text < 10)
  135. {
  136. res += "0" + text;
  137. }
  138. else
  139. {
  140. res = text.ToString();
  141. }
  142. return res;
  143. }
  144. #endregion
  145. #region 把对象类型转换成指定的类型,转化失败时返回指定默认值
  146. /// <summary>
  147. /// 把对象类型转换成指定的类型,转化失败时返回指定默认值
  148. /// </summary>
  149. /// <typeparam name="T">动态类型</typeparam>
  150. /// <param name="value">要转换的原对象</param>
  151. /// <param name="detaultValue">转换失败时返回的默认值</param>
  152. /// <returns>转化后指定类型对象,转化失败时返回指定默认值</returns>
  153. public static T CastTo<T>(this object value, T detaultValue)
  154. {
  155. object result;
  156. Type t = typeof(T);
  157. try
  158. {
  159. result = t.IsEnum ? System.Enum.Parse(t, value.ToString()) : Convert.ChangeType(value, t);
  160. }
  161. catch (Exception)
  162. {
  163. return detaultValue;
  164. }
  165. return (T)result;
  166. }
  167. #endregion
  168. #region 把对象类型转换成指定的类型,转化失败时返回类型默认值
  169. /// <summary>
  170. /// 把对象类型转换成指定的类型,转化失败时返回类型默认值
  171. /// </summary>
  172. /// <typeparam name="T">动态类型</typeparam>
  173. /// <param name="value">要转换的原对象</param>
  174. /// <returns>转化后指定类型对象,转化失败时返回类型默认值</returns>
  175. public static T CastTo<T>(this object value)
  176. {
  177. object result;
  178. Type t = typeof(T);
  179. try
  180. {
  181. if (t.IsEnum)
  182. {
  183. result = System.Enum.Parse(t, value.ToString());
  184. }
  185. else if (t == typeof(Guid))
  186. {
  187. result = Guid.Parse(value.ToString());
  188. }
  189. else
  190. {
  191. result = Convert.ChangeType(value, t);
  192. }
  193. }
  194. catch (Exception)
  195. {
  196. result = default(T);
  197. }
  198. return (T)result;
  199. }
  200. #endregion
  201. #region DataTable转换成List集合
  202. /// <summary>
  203. /// DataTable转换成List集合
  204. /// </summary>
  205. /// <typeparam name="T"></typeparam>
  206. /// <param name="dt"></param>
  207. /// <returns></returns>
  208. public static List<T> DataTableToList<T>(this DataTable dt) where T : new()
  209. {
  210. // 定义集合
  211. IList<T> ts = new List<T>();
  212. // 获得此模型的类型
  213. Type type = typeof(T);
  214. foreach (DataRow dr in dt.Rows)
  215. {
  216. T t = new T();
  217. // 获得此模型的公共属性
  218. PropertyInfo[] propertys = t.GetType().GetProperties();
  219. foreach (PropertyInfo pi in propertys)
  220. {
  221. string tempName = pi.Name;
  222. if (dt.Columns.Contains(tempName))
  223. {
  224. // 判断此属性是否有Setter
  225. if (!pi.CanWrite) continue;
  226. object value = dr[tempName];
  227. if (value != DBNull.Value)
  228. pi.SetValue(t, value, null);
  229. }
  230. }
  231. ts.Add(t);
  232. }
  233. return ts.CastTo<List<T>>();
  234. }
  235. #endregion
  236. }
  237. }