SystemExtensions.cs 8.3 KB

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