ObjectExtension.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. namespace Admin.NET.Core;
  2. /// <summary>
  3. /// 对象拓展类
  4. /// </summary>
  5. [SuppressSniffer]
  6. public static 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. /// List转DataTable
  51. /// </summary>
  52. /// <typeparam name="T"></typeparam>
  53. /// <param name="list"></param>
  54. /// <returns></returns>
  55. public static DataTable ToDataTable<T>(this List<T> list)
  56. {
  57. DataTable result = new();
  58. if (list.Count > 0)
  59. {
  60. result.TableName = list[0].GetType().Name; // 表名赋值
  61. PropertyInfo[] propertys = list[0].GetType().GetProperties();
  62. foreach (PropertyInfo pi in propertys)
  63. {
  64. Type colType = pi.PropertyType;
  65. if (colType.IsGenericType && colType.GetGenericTypeDefinition() == typeof(Nullable<>))
  66. {
  67. colType = colType.GetGenericArguments()[0];
  68. }
  69. if (IsIgnoreColumn(pi))
  70. continue;
  71. result.Columns.Add(pi.Name, colType);
  72. }
  73. for (int i = 0; i < list.Count; i++)
  74. {
  75. ArrayList tempList = new();
  76. foreach (PropertyInfo pi in propertys)
  77. {
  78. if (IsIgnoreColumn(pi))
  79. continue;
  80. object obj = pi.GetValue(list[i], null);
  81. tempList.Add(obj);
  82. }
  83. object[] array = tempList.ToArray();
  84. result.LoadDataRow(array, true);
  85. }
  86. }
  87. return result;
  88. }
  89. /// <summary>
  90. /// 对象序列化成Json字符串
  91. /// </summary>
  92. /// <param name="obj"></param>
  93. /// <returns></returns>
  94. public static string ToJson(this object obj)
  95. {
  96. return JSON.GetJsonSerializer().Serialize(obj);
  97. }
  98. /// <summary>
  99. /// Json字符串反序列化成对象
  100. /// </summary>
  101. /// <typeparam name="T"></typeparam>
  102. /// <param name="json"></param>
  103. /// <returns></returns>
  104. public static T ToObject<T>(this string json)
  105. {
  106. return JSON.GetJsonSerializer().Deserialize<T>(json);
  107. }
  108. /// <summary>
  109. /// 排除SqlSugar忽略的列
  110. /// </summary>
  111. /// <param name="pi"></param>
  112. /// <returns></returns>
  113. private static bool IsIgnoreColumn(PropertyInfo pi)
  114. {
  115. var sc = pi.GetCustomAttributes<SugarColumn>(false).FirstOrDefault(u => u.IsIgnore == true);
  116. return sc != null;
  117. }
  118. }