ObjectExtension.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. namespace Admin.NET.Core;
  2. /// <summary>
  3. /// 对象拓展
  4. /// </summary>
  5. [SuppressSniffer]
  6. public static partial class ObjectExtension
  7. {
  8. /// <summary>
  9. /// 判断类型是否实现某个泛型
  10. /// </summary>
  11. /// <param name="type">类型</param>
  12. /// <param name="generic">泛型类型</param>
  13. /// <returns>bool</returns>
  14. public static bool HasImplementedRawGeneric(this Type type, Type generic)
  15. {
  16. // 检查接口类型
  17. var isTheRawGenericType = type.GetInterfaces().Any(IsTheRawGenericType);
  18. if (isTheRawGenericType) return true;
  19. // 检查类型
  20. while (type != null && type != typeof(object))
  21. {
  22. isTheRawGenericType = IsTheRawGenericType(type);
  23. if (isTheRawGenericType) return true;
  24. type = type.BaseType;
  25. }
  26. return false;
  27. // 判断逻辑
  28. bool IsTheRawGenericType(Type type) => generic == (type.IsGenericType ? type.GetGenericTypeDefinition() : type);
  29. }
  30. /// <summary>
  31. /// 将字典转化为QueryString格式
  32. /// </summary>
  33. /// <param name="dict"></param>
  34. /// <param name="urlEncode"></param>
  35. /// <returns></returns>
  36. public static string ToQueryString(this Dictionary<string, string> dict, bool urlEncode = true)
  37. {
  38. return string.Join("&", dict.Select(p => $"{(urlEncode ? p.Key?.UrlEncode() : "")}={(urlEncode ? p.Value?.UrlEncode() : "")}"));
  39. }
  40. /// <summary>
  41. /// 将字符串URL编码
  42. /// </summary>
  43. /// <param name="str"></param>
  44. /// <returns></returns>
  45. public static string UrlEncode(this string str)
  46. {
  47. return string.IsNullOrEmpty(str) ? "" : System.Web.HttpUtility.UrlEncode(str, Encoding.UTF8);
  48. }
  49. /// <summary>
  50. /// 对象序列化成Json字符串
  51. /// </summary>
  52. /// <param name="obj"></param>
  53. /// <returns></returns>
  54. public static string ToJson(this object obj)
  55. {
  56. return JSON.GetJsonSerializer().Serialize(obj);
  57. }
  58. /// <summary>
  59. /// Json字符串反序列化成对象
  60. /// </summary>
  61. /// <typeparam name="T"></typeparam>
  62. /// <param name="json"></param>
  63. /// <returns></returns>
  64. public static T ToObject<T>(this string json)
  65. {
  66. return JSON.GetJsonSerializer().Deserialize<T>(json);
  67. }
  68. /// <summary>
  69. /// 将object转换为long,若失败则返回0
  70. /// </summary>
  71. /// <param name="obj"></param>
  72. /// <returns></returns>
  73. public static long ParseToLong(this object obj)
  74. {
  75. try
  76. {
  77. return long.Parse(obj.ToString());
  78. }
  79. catch
  80. {
  81. return 0L;
  82. }
  83. }
  84. /// <summary>
  85. /// 将object转换为long,若失败则返回指定值
  86. /// </summary>
  87. /// <param name="str"></param>
  88. /// <param name="defaultValue"></param>
  89. /// <returns></returns>
  90. public static long ParseToLong(this string str, long defaultValue)
  91. {
  92. try
  93. {
  94. return long.Parse(str);
  95. }
  96. catch
  97. {
  98. return defaultValue;
  99. }
  100. }
  101. /// <summary>
  102. /// 将object转换为double,若失败则返回0
  103. /// </summary>
  104. /// <param name="obj"></param>
  105. /// <returns></returns>
  106. public static double ParseToDouble(this object obj)
  107. {
  108. try
  109. {
  110. return double.Parse(obj.ToString());
  111. }
  112. catch
  113. {
  114. return 0;
  115. }
  116. }
  117. /// <summary>
  118. /// 将object转换为double,若失败则返回指定值
  119. /// </summary>
  120. /// <param name="str"></param>
  121. /// <param name="defaultValue"></param>
  122. /// <returns></returns>
  123. public static double ParseToDouble(this object str, double defaultValue)
  124. {
  125. try
  126. {
  127. return double.Parse(str.ToString());
  128. }
  129. catch
  130. {
  131. return defaultValue;
  132. }
  133. }
  134. /// <summary>
  135. /// 将string转换为DateTime,若失败则返回日期最小值
  136. /// </summary>
  137. /// <param name="str"></param>
  138. /// <returns></returns>
  139. public static DateTime ParseToDateTime(this string str)
  140. {
  141. try
  142. {
  143. if (string.IsNullOrWhiteSpace(str))
  144. {
  145. return DateTime.MinValue;
  146. }
  147. if (str.Contains('-') || str.Contains('/'))
  148. {
  149. return DateTime.Parse(str);
  150. }
  151. else
  152. {
  153. int length = str.Length;
  154. switch (length)
  155. {
  156. case 4:
  157. return DateTime.ParseExact(str, "yyyy", System.Globalization.CultureInfo.CurrentCulture);
  158. case 6:
  159. return DateTime.ParseExact(str, "yyyyMM", System.Globalization.CultureInfo.CurrentCulture);
  160. case 8:
  161. return DateTime.ParseExact(str, "yyyyMMdd", System.Globalization.CultureInfo.CurrentCulture);
  162. case 10:
  163. return DateTime.ParseExact(str, "yyyyMMddHH", System.Globalization.CultureInfo.CurrentCulture);
  164. case 12:
  165. return DateTime.ParseExact(str, "yyyyMMddHHmm", System.Globalization.CultureInfo.CurrentCulture);
  166. case 14:
  167. return DateTime.ParseExact(str, "yyyyMMddHHmmss", System.Globalization.CultureInfo.CurrentCulture);
  168. default:
  169. return DateTime.ParseExact(str, "yyyyMMddHHmmss", System.Globalization.CultureInfo.CurrentCulture);
  170. }
  171. }
  172. }
  173. catch
  174. {
  175. return DateTime.MinValue;
  176. }
  177. }
  178. /// <summary>
  179. /// 将string转换为DateTime,若失败则返回默认值
  180. /// </summary>
  181. /// <param name="str"></param>
  182. /// <param name="defaultValue"></param>
  183. /// <returns></returns>
  184. public static DateTime ParseToDateTime(this string str, DateTime? defaultValue)
  185. {
  186. try
  187. {
  188. if (string.IsNullOrWhiteSpace(str))
  189. {
  190. return defaultValue.GetValueOrDefault();
  191. }
  192. if (str.Contains('-') || str.Contains('/'))
  193. {
  194. return DateTime.Parse(str);
  195. }
  196. else
  197. {
  198. int length = str.Length;
  199. switch (length)
  200. {
  201. case 4:
  202. return DateTime.ParseExact(str, "yyyy", System.Globalization.CultureInfo.CurrentCulture);
  203. case 6:
  204. return DateTime.ParseExact(str, "yyyyMM", System.Globalization.CultureInfo.CurrentCulture);
  205. case 8:
  206. return DateTime.ParseExact(str, "yyyyMMdd", System.Globalization.CultureInfo.CurrentCulture);
  207. case 10:
  208. return DateTime.ParseExact(str, "yyyyMMddHH", System.Globalization.CultureInfo.CurrentCulture);
  209. case 12:
  210. return DateTime.ParseExact(str, "yyyyMMddHHmm", System.Globalization.CultureInfo.CurrentCulture);
  211. case 14:
  212. return DateTime.ParseExact(str, "yyyyMMddHHmmss", System.Globalization.CultureInfo.CurrentCulture);
  213. default:
  214. return DateTime.ParseExact(str, "yyyyMMddHHmmss", System.Globalization.CultureInfo.CurrentCulture);
  215. }
  216. }
  217. }
  218. catch
  219. {
  220. return defaultValue.GetValueOrDefault();
  221. }
  222. }
  223. }