ObjectExtension.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. using Furion.DependencyInjection;
  2. using SqlSugar;
  3. using System;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using System.Data;
  7. using System.Linq;
  8. using System.Reflection;
  9. using System.Text;
  10. namespace Admin.NET.Core
  11. {
  12. /// <summary>
  13. /// 对象拓展类
  14. /// </summary>
  15. [SuppressSniffer]
  16. public static class ObjectExtension
  17. {
  18. /// <summary>
  19. /// 判断类型是否实现某个泛型
  20. /// </summary>
  21. /// <param name="type">类型</param>
  22. /// <param name="generic">泛型类型</param>
  23. /// <returns>bool</returns>
  24. public static bool HasImplementedRawGeneric(this Type type, Type generic)
  25. {
  26. // 检查接口类型
  27. var isTheRawGenericType = type.GetInterfaces().Any(IsTheRawGenericType);
  28. if (isTheRawGenericType) return true;
  29. // 检查类型
  30. while (type != null && type != typeof(object))
  31. {
  32. isTheRawGenericType = IsTheRawGenericType(type);
  33. if (isTheRawGenericType) return true;
  34. type = type.BaseType;
  35. }
  36. return false;
  37. // 判断逻辑
  38. bool IsTheRawGenericType(Type type) => generic == (type.IsGenericType ? type.GetGenericTypeDefinition() : type);
  39. }
  40. /// <summary>
  41. /// 将字典转化为QueryString格式
  42. /// </summary>
  43. /// <param name="dict"></param>
  44. /// <param name="urlEncode"></param>
  45. /// <returns></returns>
  46. public static string ToQueryString(this Dictionary<string, string> dict, bool urlEncode = true)
  47. {
  48. return string.Join("&", dict.Select(p => $"{(urlEncode ? p.Key?.UrlEncode() : "")}={(urlEncode ? p.Value?.UrlEncode() : "")}"));
  49. }
  50. /// <summary>
  51. /// 将字符串URL编码
  52. /// </summary>
  53. /// <param name="str"></param>
  54. /// <returns></returns>
  55. public static string UrlEncode(this string str)
  56. {
  57. return string.IsNullOrEmpty(str) ? "" : System.Web.HttpUtility.UrlEncode(str, Encoding.UTF8);
  58. }
  59. /// <summary>
  60. /// List转DataTable
  61. /// </summary>
  62. /// <typeparam name="T"></typeparam>
  63. /// <param name="list"></param>
  64. /// <returns></returns>
  65. public static DataTable ToDataTable<T>(this List<T> list)
  66. {
  67. DataTable result = new();
  68. if (list.Count > 0)
  69. {
  70. result.TableName = list[0].GetType().Name; // 表名赋值
  71. PropertyInfo[] propertys = list[0].GetType().GetProperties();
  72. foreach (PropertyInfo pi in propertys)
  73. {
  74. Type colType = pi.PropertyType;
  75. if (colType.IsGenericType && colType.GetGenericTypeDefinition() == typeof(Nullable<>))
  76. {
  77. colType = colType.GetGenericArguments()[0];
  78. }
  79. if (IsIgnoreColumn(pi))
  80. continue;
  81. result.Columns.Add(pi.Name, colType);
  82. }
  83. for (int i = 0; i < list.Count; i++)
  84. {
  85. ArrayList tempList = new();
  86. foreach (PropertyInfo pi in propertys)
  87. {
  88. if (IsIgnoreColumn(pi))
  89. continue;
  90. object obj = pi.GetValue(list[i], null);
  91. tempList.Add(obj);
  92. }
  93. object[] array = tempList.ToArray();
  94. result.LoadDataRow(array, true);
  95. }
  96. }
  97. return result;
  98. }
  99. /// <summary>
  100. /// 排除SqlSugar忽略的列
  101. /// </summary>
  102. /// <param name="pi"></param>
  103. /// <returns></returns>
  104. private static bool IsIgnoreColumn(PropertyInfo pi)
  105. {
  106. var sc = pi.GetCustomAttributes<SugarColumn>(false).FirstOrDefault(u => u.IsIgnore == true);
  107. return sc != null;
  108. }
  109. }
  110. }