MethodExtensions.cs 7.0 KB

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