MethodExtensions.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Linq;
  6. using System.Reflection;
  7. namespace WWPipeLine.Commons
  8. {
  9. public static class MethodExtensions
  10. {
  11. /// <summary>
  12. /// 获取枚举描述信息
  13. /// </summary>
  14. /// <typeparam name="T"></typeparam>
  15. /// <param name="eunmObj"></param>
  16. /// <returns></returns>
  17. public static string GetEnumDescription<T>(T eunmObj)
  18. {
  19. //获取枚举对象的枚举类型
  20. var type = eunmObj.GetType();
  21. //通过反射获取该枚举类型的所有属性
  22. var fieldInfos = type.GetFields();
  23. foreach (var field in fieldInfos)
  24. {
  25. //不是参数obj,就直接跳过
  26. if (field.Name != eunmObj.ToString())
  27. {
  28. continue;
  29. }
  30. //取出参数obj的自定义属性
  31. if (!field.IsDefined(typeof(DescriptionAttribute), true)) continue;
  32. var descriptionAttribute = field.GetCustomAttributes(typeof(DescriptionAttribute),
  33. true)[0] as DescriptionAttribute;
  34. if (descriptionAttribute != null)
  35. return descriptionAttribute.Description;
  36. }
  37. return eunmObj.ToString();
  38. }
  39. /// <summary>
  40. /// 获取属性值
  41. /// </summary>
  42. /// <param name="obj">对象</param>
  43. /// <param name="property">属性</param>
  44. /// <returns></returns>
  45. public static object GetPropertyValue(this object obj, string property)
  46. {
  47. try
  48. {
  49. if (obj == null)
  50. {
  51. return null;
  52. }
  53. if (string.IsNullOrEmpty(property))
  54. {
  55. return null;
  56. }
  57. // 获取属性
  58. PropertyInfo propertyInfo = obj.GetType().GetProperty(property);
  59. if (propertyInfo == null)
  60. {
  61. return null;
  62. }
  63. // 返回值
  64. return propertyInfo.GetValue(obj, null);
  65. }
  66. catch (Exception ex)
  67. {
  68. throw new Exception(string.Format("反射获取对象字段值异常 GetPropertyValue,obj:{0}\r\n property{1} ", obj.ToString(), property), ex);
  69. }
  70. }
  71. /// <summary>
  72. /// 设置对象属性值
  73. /// </summary>
  74. /// <param name="obj"对象></param>
  75. /// <param name="property">属性</param>
  76. /// <param name="value">值</param>
  77. public static void SetPropertyValue(this object obj, string property, object value)
  78. {
  79. try
  80. {
  81. PropertyInfo propertyInfo = obj.GetType().GetProperty(property);
  82. if (propertyInfo == null)
  83. {
  84. return;
  85. }
  86. propertyInfo.SetValue(obj, value, null);
  87. }
  88. catch (Exception ex)
  89. {
  90. throw new Exception(string.Format("反射设置对象字段值异常 SetPropertyValue,obj:{0}\r\n property{1} \r\n value{2}", obj.ToString(), property, value), ex);
  91. }
  92. }
  93. #region 时间转换
  94. /// <summary>
  95. /// 字符串时间格式转日期时间统一字符格式
  96. /// 格式为:"yyyy-MM-dd"
  97. /// </summary>
  98. /// <param name="strDateTime"></param>
  99. /// <returns></returns>
  100. public static string ToDateString(this string strDateTime)
  101. {
  102. DateTime dt;
  103. var _isOk = DateTime.TryParse(strDateTime, out dt);
  104. if (_isOk == false)
  105. {
  106. return "";// throw new Exception(string.Format("字符串转时间错误.字符串为:{0}", strDateTime));
  107. }
  108. return dt.ToDateString();
  109. }
  110. /// <summary>
  111. /// 获取格式化字符串,不带时分秒,格式:"yyyy-MM-dd"
  112. /// </summary>
  113. /// <param name="dateTime">日期</param>
  114. public static string ToDateString(this DateTime dateTime)
  115. {
  116. return dateTime.ToString("yyyy-MM-dd");
  117. }
  118. /// <summary>
  119. /// 获取格式化字符串,不带时分秒,格式:"yyyy-MM-dd"
  120. /// </summary>
  121. /// <param name="dateTime">日期</param>
  122. public static string ToDateString(this DateTime? dateTime)
  123. {
  124. if (dateTime == null)
  125. return string.Empty;
  126. return ToDateString(dateTime.Value);
  127. }
  128. /// <summary>
  129. /// 时间格式转字符串
  130. /// 字符串格式"yyyy-MM-dd HH:mm:ss.fff"
  131. /// </summary>
  132. /// <param name="dt"></param>
  133. /// <returns></returns>
  134. public static string ToLongString(this DateTime dt)
  135. {
  136. return dt.ToString("yyyy-MM-dd HH:mm:ss.fff");
  137. }
  138. /// <summary>
  139. /// 字符串时间格式转时间统一字符格式
  140. /// 格式为:"yyyy-MM-dd HH:mm:ss.fff"
  141. /// </summary>
  142. /// <param name="strDateTime"></param>
  143. /// <returns></returns>
  144. public static string ToLongString(this string strDateTime)
  145. {
  146. DateTime dt;
  147. var _isOk = DateTime.TryParse(strDateTime, out dt);
  148. if (_isOk == false)
  149. {
  150. return "";// throw new Exception(string.Format("字符串转时间错误.字符串为:{0}", strDateTime));
  151. }
  152. return dt.ToString("yyyy-MM-dd HH:mm:ss.fff");
  153. }
  154. /// <summary>
  155. /// 字符串转时间格式
  156. /// </summary>
  157. /// <param name="dt"></param>
  158. /// <returns></returns>
  159. public static DateTime ToDateTime(this string dt)
  160. {
  161. DateTime result = DateTime.MinValue;
  162. DateTime.TryParse(dt, out result);
  163. return result;
  164. }
  165. #region 时间戳
  166. /// <summary>
  167. /// DateTime时间格式转换为Unix时间戳格式
  168. /// </summary>
  169. /// <param name="time"> DateTime时间格式</param>
  170. /// <returns>Unix时间戳格式</returns>
  171. public static string ToTimeStamp(this System.DateTime time)
  172. {
  173. return DateTime.Now.ToString("yyyyMMddhhmmssfff");
  174. }
  175. /// <summary>
  176. /// 时间戳转时间
  177. /// </summary>
  178. /// <param name="timeStamp">Unix时间戳</param>
  179. /// <returns></returns>
  180. public static DateTime TimeStampToTime(this string timeStamp)
  181. {
  182. DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
  183. long lTime = long.Parse(timeStamp);
  184. TimeSpan toNow = new TimeSpan(lTime);
  185. return dtStart.Add(toNow);
  186. }
  187. #endregion 时间戳
  188. #endregion 时间转换
  189. #region DataTable 转换
  190. /// <summary>
  191. /// 转化一个DataTable
  192. /// </summary>
  193. /// <typeparam name="T"></typeparam>
  194. /// <param name="list"></param>
  195. /// <returns></returns>
  196. public static DataTable ListToDataTable<T>(this List<T> list)
  197. {
  198. //创建属性的集合
  199. List<PropertyInfo> pList = new List<PropertyInfo>();
  200. Type type = typeof(T);
  201. //获得反射的入口
  202. if (list != null && list.Any())
  203. {
  204. type = list.First().GetType();
  205. }
  206. else
  207. {
  208. return new DataTable();
  209. }
  210. DataTable dt = new DataTable();
  211. //把所有的public属性加入到集合 并添加DataTable的列
  212. Array.ForEach<PropertyInfo>(type.GetProperties(), p => { pList.Add(p); dt.Columns.Add(p.Name, p.PropertyType); });
  213. foreach (var item in list)
  214. {
  215. //创建一个DataRow实例
  216. DataRow row = dt.NewRow();
  217. //给row 赋值
  218. pList.ForEach(p => row[p.Name] = p.GetValue(item, null));
  219. //加入到DataTable
  220. dt.Rows.Add(row);
  221. }
  222. return dt;
  223. }
  224. #endregion DataTable 转换
  225. }
  226. }