SystemExtensions.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Text.RegularExpressions;
  5. namespace FCS.Common
  6. {
  7. /// <summary>
  8. /// 系统扩展方法
  9. /// </summary>
  10. public static class SystemExtensions
  11. {
  12. #region 字符串类扩展方法
  13. /// <summary>
  14. /// 生成手机号码的友好显示
  15. /// 形如133****1234
  16. /// </summary>
  17. /// <param name="s">电话号码</param>
  18. /// <returns></returns>
  19. public static string ToMobileNumReplaceShow(this string s)
  20. {
  21. if (string.IsNullOrWhiteSpace(s))
  22. return string.Empty;
  23. s = s.Trim();
  24. Regex reg = new Regex(@"^1[34578]\d{9}$");
  25. if (reg.IsMatch(s))
  26. s = s.Remove(3, 4).Insert(3, "****");
  27. return s;
  28. }
  29. /// <summary>
  30. /// 该字符串是否为电话号码
  31. /// </summary>
  32. /// <param name="s">字符串</param>
  33. /// <returns></returns>
  34. public static bool IsMobileString(this string s)
  35. {
  36. if (string.IsNullOrWhiteSpace(s)) return false;
  37. s = s.Trim();
  38. Regex reg = new Regex(@"^1[34578]\d{9}$");
  39. return reg.IsMatch(s);
  40. }
  41. /// <summary>
  42. /// 将string[]类型转换成string(用|分隔)
  43. /// </summary>
  44. /// <param name="str"></param>
  45. /// <returns></returns>
  46. public static string ArrayToString(this string[] str)
  47. {
  48. if (str == null)
  49. return "";
  50. StringBuilder sb = new StringBuilder(str.Length * 50);
  51. foreach (var item in str)
  52. {
  53. sb.Append(item + "|");
  54. }
  55. return sb.ToString().TrimEnd('|');
  56. }
  57. #endregion 字符串类扩展方法
  58. #region 时间类扩展方法
  59. /// <summary>
  60. /// 生成时间的友好提示
  61. /// 如:刚刚,多少秒前,多少小时前,多少天前
  62. /// </summary>
  63. /// <param name="dateTime">生成友好提示的时间对象</param>
  64. /// <param name="dateFormat">日期格式(可空,默认yyyy-MM-dd)</param>
  65. /// <param name="showTime">是否显示时间(可控,默认显示,格式HH:mm)</param>
  66. /// <returns></returns>
  67. public static string ToFriendlyString(this DateTime dateTime, string dateFormat = "", bool showTime = true)
  68. {
  69. if (dateTime == DateTime.MinValue)
  70. return "-";
  71. if (string.IsNullOrEmpty(dateFormat))
  72. dateFormat = "yyyy-MM-dd";
  73. string timeFormat = "HH:mm";
  74. DateTime userDate = dateTime;
  75. DateTime userNow = DateTime.Now;
  76. if (userDate > userNow)
  77. {
  78. return userDate.ToString(dateFormat + (showTime ? " " + timeFormat : ""));
  79. }
  80. TimeSpan intervalTime = userNow - userDate;
  81. int intervalDays;
  82. if (userNow.Year == userDate.Year)
  83. intervalDays = userNow.DayOfYear - userDate.DayOfYear;
  84. else
  85. intervalDays = intervalTime.Days + 1;
  86. string result = "{0}";
  87. if (showTime)
  88. result = "{0}" + " " + userDate.ToString(timeFormat);
  89. if (intervalDays > 7)
  90. {
  91. if (userDate.Year == userNow.Year)
  92. {
  93. return string.Format("{0}月{1}日{2}",
  94. userDate.Month,
  95. userDate.Day,
  96. showTime ? " " + userDate.ToString(timeFormat) : "");
  97. }
  98. return userDate.ToString(dateFormat + (showTime ? " " + timeFormat : ""));
  99. }
  100. if (intervalDays >= 3)
  101. {
  102. string timeScope = string.Format("{0}天之前", intervalDays);
  103. return string.Format(result, timeScope);
  104. }
  105. if (intervalDays == 2)
  106. {
  107. return string.Format(result, "前天");
  108. }
  109. if (intervalDays == 1)
  110. {
  111. return string.Format(result, "昨天");
  112. }
  113. if (intervalTime.Hours >= 1)
  114. return string.Format("{0}小时之前", intervalTime.Hours);
  115. if (intervalTime.Minutes >= 1)
  116. return string.Format("{0}分钟之前", intervalTime.Minutes);
  117. if (intervalTime.Seconds >= 1)
  118. return string.Format("{0}秒之前", intervalTime.Seconds);
  119. return "现在";
  120. }
  121. #endregion 时间类扩展方法
  122. #region 字典类扩展方法
  123. /// <summary>
  124. /// 从字典中获取key的值,若不存在或返回指定类型失败,则返回指定类型的默认值
  125. /// </summary>
  126. /// <typeparam name="T">指定返回类型</typeparam>
  127. /// <param name="dictionary">字典</param>
  128. /// <param name="key">获取的key</param>
  129. /// <param name="defaultValue">指定返回类型的默认值</param>
  130. /// <returns></returns>
  131. public static T Get<T>(this IDictionary<string, object> dictionary, string key, T defaultValue)
  132. {
  133. if (dictionary.ContainsKey(key))
  134. {
  135. object obj2;
  136. dictionary.TryGetValue(key, out obj2);
  137. return ChangeType<T>(obj2, defaultValue);
  138. }
  139. return defaultValue;
  140. }
  141. /// <summary>
  142. /// 向字典中尝试添加键值
  143. /// </summary>
  144. /// <typeparam name="TKey">该字典键类型</typeparam>
  145. /// <typeparam name="TValue">该字典值类型</typeparam>
  146. /// <param name="dictionary">字典</param>
  147. /// <param name="key">键</param>
  148. /// <param name="value">值</param>
  149. /// <returns></returns>
  150. public static IDictionary<TKey, TValue> TryAdd<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, TValue value)
  151. {
  152. if (value != null && !dictionary.ContainsKey(key))
  153. {
  154. dictionary[key] = value;
  155. }
  156. return dictionary;
  157. }
  158. #endregion 字典类扩展方法
  159. #region 强制类型转换
  160. public static T ChangeType<T>(object value)
  161. {
  162. return ChangeType<T>(value, default(T));
  163. }
  164. public static T ChangeType<T>(object value, T defalutValue)
  165. {
  166. if (value != null)
  167. {
  168. Type nullableType = typeof(T);
  169. if (!nullableType.IsInterface && (!nullableType.IsClass || (nullableType == typeof(string))))
  170. {
  171. if (nullableType.IsGenericType && (nullableType.GetGenericTypeDefinition() == typeof(Nullable<>)))
  172. {
  173. return (T)Convert.ChangeType(value, Nullable.GetUnderlyingType(nullableType));
  174. }
  175. if (nullableType.IsEnum)
  176. {
  177. return (T)Enum.Parse(nullableType, value.ToString());
  178. }
  179. return (T)Convert.ChangeType(value, nullableType);
  180. }
  181. if (value is T)
  182. {
  183. return (T)value;
  184. }
  185. }
  186. return defalutValue;
  187. }
  188. #endregion 强制类型转换
  189. #region 本系统时间扩展
  190. /// <summary>
  191. /// 生成指定的时间字符串
  192. /// yyyy-MM-dd HH:mm:ss
  193. /// </summary>
  194. /// <param name="datetime"></param>
  195. /// <returns></returns>
  196. public static string ToFCSDateTimeString1(this DateTime datetime)
  197. {
  198. return datetime.ToString("yyyy-MM-dd HH:mm:ss");
  199. }
  200. /// <summary>
  201. /// 生成指定的时间字符串
  202. /// yyyyMMddHHmmss
  203. /// </summary>
  204. /// <param name="datetime"></param>
  205. /// <returns></returns>
  206. public static string ToFCSDateTimeString2(this DateTime datetime)
  207. {
  208. return datetime.ToString("yyyyMMddHHmmss");
  209. }
  210. /// <summary>
  211. /// 生成指定的时间字符串
  212. /// yyyy-MM-dd
  213. /// </summary>
  214. /// <param name="datetime"></param>
  215. /// <returns></returns>
  216. public static string ToFCSDateTimeString3(this DateTime datetime)
  217. {
  218. return datetime.ToString("yyyy-MM-dd");
  219. }
  220. #endregion 本系统时间扩展
  221. }
  222. }