123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247 |
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Linq;
- using System.Reflection;
- namespace WWPipeLine.Commons
- {
- public static class MethodExtensions
- {
-
-
-
-
-
-
- public static string GetEnumDescription<T>(T eunmObj)
- {
-
- var type = eunmObj.GetType();
-
- var fieldInfos = type.GetFields();
- foreach (var field in fieldInfos)
- {
-
- if (field.Name != eunmObj.ToString())
- {
- continue;
- }
-
- if (!field.IsDefined(typeof(DescriptionAttribute), true)) continue;
- var descriptionAttribute = field.GetCustomAttributes(typeof(DescriptionAttribute),
- true)[0] as DescriptionAttribute;
- if (descriptionAttribute != null)
- return descriptionAttribute.Description;
- }
- return eunmObj.ToString();
- }
-
-
-
-
-
-
- public static object GetPropertyValue(this object obj, string property)
- {
- try
- {
- if (obj == null)
- {
- return null;
- }
- if (string.IsNullOrEmpty(property))
- {
- return null;
- }
-
- PropertyInfo propertyInfo = obj.GetType().GetProperty(property);
- if (propertyInfo == null)
- {
- return null;
- }
-
- return propertyInfo.GetValue(obj, null);
- }
- catch (Exception ex)
- {
- throw new Exception(string.Format("反射获取对象字段值异常 GetPropertyValue,obj:{0}\r\n property{1} ", obj.ToString(), property), ex);
- }
- }
-
-
-
-
-
-
- public static void SetPropertyValue(this object obj, string property, object value)
- {
- try
- {
- PropertyInfo propertyInfo = obj.GetType().GetProperty(property);
- if (propertyInfo == null)
- {
- return;
- }
- propertyInfo.SetValue(obj, value, null);
- }
- catch (Exception ex)
- {
- throw new Exception(string.Format("反射设置对象字段值异常 SetPropertyValue,obj:{0}\r\n property{1} \r\n value{2}", obj.ToString(), property, value), ex);
- }
- }
- #region 时间转换
-
-
-
-
-
-
- public static string ToDateString(this string strDateTime)
- {
- DateTime dt;
- var _isOk = DateTime.TryParse(strDateTime, out dt);
- if (_isOk == false)
- {
- return "";
- }
- return dt.ToDateString();
- }
-
-
-
-
- public static string ToDateString(this DateTime dateTime)
- {
- return dateTime.ToString("yyyy-MM-dd");
- }
-
-
-
-
- public static string ToDateString(this DateTime? dateTime)
- {
- if (dateTime == null)
- return string.Empty;
- return ToDateString(dateTime.Value);
- }
-
-
-
-
-
-
- public static string ToLongString(this DateTime dt)
- {
- return dt.ToString("yyyy-MM-dd HH:mm:ss.fff");
- }
-
-
-
-
-
-
- public static string ToLongString(this string strDateTime)
- {
- DateTime dt;
- var _isOk = DateTime.TryParse(strDateTime, out dt);
- if (_isOk == false)
- {
- return "";
- }
- return dt.ToString("yyyy-MM-dd HH:mm:ss.fff");
- }
-
-
-
-
-
- public static DateTime ToDateTime(this string dt)
- {
- DateTime result = DateTime.MinValue;
- DateTime.TryParse(dt, out result);
- return result;
- }
- #region 时间戳
-
-
-
-
-
- public static string ToTimeStamp(this System.DateTime time)
- {
- return DateTime.Now.ToString("yyyyMMddhhmmssfff");
- }
-
-
-
-
-
- public static DateTime TimeStampToTime(this string timeStamp)
- {
- DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
- long lTime = long.Parse(timeStamp);
- TimeSpan toNow = new TimeSpan(lTime);
- return dtStart.Add(toNow);
- }
- #endregion 时间戳
- #endregion 时间转换
- #region DataTable 转换
-
-
-
-
-
-
- public static DataTable ListToDataTable<T>(this List<T> list)
- {
-
- List<PropertyInfo> pList = new List<PropertyInfo>();
- Type type = typeof(T);
-
- if (list != null && list.Any())
- {
- type = list.First().GetType();
- }
- else
- {
- return new DataTable();
- }
- DataTable dt = new DataTable();
-
- Array.ForEach<PropertyInfo>(type.GetProperties(), p => { pList.Add(p); dt.Columns.Add(p.Name, p.PropertyType); });
- foreach (var item in list)
- {
-
- DataRow row = dt.NewRow();
-
- pList.ForEach(p => row[p.Name] = p.GetValue(item, null));
-
- dt.Rows.Add(row);
- }
- return dt;
- }
- #endregion DataTable 转换
- }
- }
|