ObjectExtension.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. // 麻省理工学院许可证
  2. //
  3. // 版权所有 (c) 2021-2023 zuohuaijun,大名科技(天津)有限公司 联系电话/微信:18020030720 QQ:515096995
  4. //
  5. // 特此免费授予获得本软件的任何人以处理本软件的权利,但须遵守以下条件:在所有副本或重要部分的软件中必须包括上述版权声明和本许可声明。
  6. //
  7. // 软件按“原样”提供,不提供任何形式的明示或暗示的保证,包括但不限于对适销性、适用性和非侵权的保证。
  8. // 在任何情况下,作者或版权持有人均不对任何索赔、损害或其他责任负责,无论是因合同、侵权或其他方式引起的,与软件或其使用或其他交易有关。
  9. namespace Admin.NET.Core;
  10. /// <summary>
  11. /// 对象拓展
  12. /// </summary>
  13. [SuppressSniffer]
  14. public static partial class ObjectExtension
  15. {
  16. /// <summary>
  17. /// 判断类型是否实现某个泛型
  18. /// </summary>
  19. /// <param name="type">类型</param>
  20. /// <param name="generic">泛型类型</param>
  21. /// <returns>bool</returns>
  22. public static bool HasImplementedRawGeneric(this Type type, Type generic)
  23. {
  24. // 检查接口类型
  25. var isTheRawGenericType = type.GetInterfaces().Any(IsTheRawGenericType);
  26. if (isTheRawGenericType) return true;
  27. // 检查类型
  28. while (type != null && type != typeof(object))
  29. {
  30. isTheRawGenericType = IsTheRawGenericType(type);
  31. if (isTheRawGenericType) return true;
  32. type = type.BaseType;
  33. }
  34. return false;
  35. // 判断逻辑
  36. bool IsTheRawGenericType(Type type) => generic == (type.IsGenericType ? type.GetGenericTypeDefinition() : type);
  37. }
  38. /// <summary>
  39. /// 将字典转化为QueryString格式
  40. /// </summary>
  41. /// <param name="dict"></param>
  42. /// <param name="urlEncode"></param>
  43. /// <returns></returns>
  44. public static string ToQueryString(this Dictionary<string, string> dict, bool urlEncode = true)
  45. {
  46. return string.Join("&", dict.Select(p => $"{(urlEncode ? p.Key?.UrlEncode() : "")}={(urlEncode ? p.Value?.UrlEncode() : "")}"));
  47. }
  48. /// <summary>
  49. /// 将字符串URL编码
  50. /// </summary>
  51. /// <param name="str"></param>
  52. /// <returns></returns>
  53. public static string UrlEncode(this string str)
  54. {
  55. return string.IsNullOrEmpty(str) ? "" : System.Web.HttpUtility.UrlEncode(str, Encoding.UTF8);
  56. }
  57. /// <summary>
  58. /// 对象序列化成Json字符串
  59. /// </summary>
  60. /// <param name="obj"></param>
  61. /// <returns></returns>
  62. public static string ToJson(this object obj)
  63. {
  64. return JSON.GetJsonSerializer().Serialize(obj);
  65. }
  66. /// <summary>
  67. /// Json字符串反序列化成对象
  68. /// </summary>
  69. /// <typeparam name="T"></typeparam>
  70. /// <param name="json"></param>
  71. /// <returns></returns>
  72. public static T ToObject<T>(this string json)
  73. {
  74. return JSON.GetJsonSerializer().Deserialize<T>(json);
  75. }
  76. /// <summary>
  77. /// 将object转换为long,若失败则返回0
  78. /// </summary>
  79. /// <param name="obj"></param>
  80. /// <returns></returns>
  81. public static long ParseToLong(this object obj)
  82. {
  83. try
  84. {
  85. return long.Parse(obj.ToString());
  86. }
  87. catch
  88. {
  89. return 0L;
  90. }
  91. }
  92. /// <summary>
  93. /// 将object转换为long,若失败则返回指定值
  94. /// </summary>
  95. /// <param name="str"></param>
  96. /// <param name="defaultValue"></param>
  97. /// <returns></returns>
  98. public static long ParseToLong(this string str, long defaultValue)
  99. {
  100. try
  101. {
  102. return long.Parse(str);
  103. }
  104. catch
  105. {
  106. return defaultValue;
  107. }
  108. }
  109. /// <summary>
  110. /// 将object转换为double,若失败则返回0
  111. /// </summary>
  112. /// <param name="obj"></param>
  113. /// <returns></returns>
  114. public static double ParseToDouble(this object obj)
  115. {
  116. try
  117. {
  118. return double.Parse(obj.ToString());
  119. }
  120. catch
  121. {
  122. return 0;
  123. }
  124. }
  125. /// <summary>
  126. /// 将object转换为double,若失败则返回指定值
  127. /// </summary>
  128. /// <param name="str"></param>
  129. /// <param name="defaultValue"></param>
  130. /// <returns></returns>
  131. public static double ParseToDouble(this object str, double defaultValue)
  132. {
  133. try
  134. {
  135. return double.Parse(str.ToString());
  136. }
  137. catch
  138. {
  139. return defaultValue;
  140. }
  141. }
  142. /// <summary>
  143. /// 将string转换为DateTime,若失败则返回日期最小值
  144. /// </summary>
  145. /// <param name="str"></param>
  146. /// <returns></returns>
  147. public static DateTime ParseToDateTime(this string str)
  148. {
  149. try
  150. {
  151. if (string.IsNullOrWhiteSpace(str))
  152. {
  153. return DateTime.MinValue;
  154. }
  155. if (str.Contains('-') || str.Contains('/'))
  156. {
  157. return DateTime.Parse(str);
  158. }
  159. else
  160. {
  161. int length = str.Length;
  162. switch (length)
  163. {
  164. case 4:
  165. return DateTime.ParseExact(str, "yyyy", System.Globalization.CultureInfo.CurrentCulture);
  166. case 6:
  167. return DateTime.ParseExact(str, "yyyyMM", System.Globalization.CultureInfo.CurrentCulture);
  168. case 8:
  169. return DateTime.ParseExact(str, "yyyyMMdd", System.Globalization.CultureInfo.CurrentCulture);
  170. case 10:
  171. return DateTime.ParseExact(str, "yyyyMMddHH", System.Globalization.CultureInfo.CurrentCulture);
  172. case 12:
  173. return DateTime.ParseExact(str, "yyyyMMddHHmm", System.Globalization.CultureInfo.CurrentCulture);
  174. case 14:
  175. return DateTime.ParseExact(str, "yyyyMMddHHmmss", System.Globalization.CultureInfo.CurrentCulture);
  176. default:
  177. return DateTime.ParseExact(str, "yyyyMMddHHmmss", System.Globalization.CultureInfo.CurrentCulture);
  178. }
  179. }
  180. }
  181. catch
  182. {
  183. return DateTime.MinValue;
  184. }
  185. }
  186. /// <summary>
  187. /// 将string转换为DateTime,若失败则返回默认值
  188. /// </summary>
  189. /// <param name="str"></param>
  190. /// <param name="defaultValue"></param>
  191. /// <returns></returns>
  192. public static DateTime ParseToDateTime(this string str, DateTime? defaultValue)
  193. {
  194. try
  195. {
  196. if (string.IsNullOrWhiteSpace(str))
  197. {
  198. return defaultValue.GetValueOrDefault();
  199. }
  200. if (str.Contains('-') || str.Contains('/'))
  201. {
  202. return DateTime.Parse(str);
  203. }
  204. else
  205. {
  206. int length = str.Length;
  207. switch (length)
  208. {
  209. case 4:
  210. return DateTime.ParseExact(str, "yyyy", System.Globalization.CultureInfo.CurrentCulture);
  211. case 6:
  212. return DateTime.ParseExact(str, "yyyyMM", System.Globalization.CultureInfo.CurrentCulture);
  213. case 8:
  214. return DateTime.ParseExact(str, "yyyyMMdd", System.Globalization.CultureInfo.CurrentCulture);
  215. case 10:
  216. return DateTime.ParseExact(str, "yyyyMMddHH", System.Globalization.CultureInfo.CurrentCulture);
  217. case 12:
  218. return DateTime.ParseExact(str, "yyyyMMddHHmm", System.Globalization.CultureInfo.CurrentCulture);
  219. case 14:
  220. return DateTime.ParseExact(str, "yyyyMMddHHmmss", System.Globalization.CultureInfo.CurrentCulture);
  221. default:
  222. return DateTime.ParseExact(str, "yyyyMMddHHmmss", System.Globalization.CultureInfo.CurrentCulture);
  223. }
  224. }
  225. }
  226. catch
  227. {
  228. return defaultValue.GetValueOrDefault();
  229. }
  230. }
  231. /// <summary>
  232. /// 判断是否有值
  233. /// </summary>
  234. /// <param name="obj"></param>
  235. /// <returns></returns>
  236. public static bool IsNullOrEmpty(this object obj)
  237. {
  238. return obj == null || string.IsNullOrEmpty(obj.ToString());
  239. }
  240. }