ObjectExtension.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. /// <returns>int</returns>
  23. public static int GetSqlSugarEntityOrder(this Type type)
  24. {
  25. return !type.IsDefined(typeof(SqlSugarEntityAttribute), true) ? 0 : type.GetCustomAttribute<SqlSugarEntityAttribute>(true).Order;
  26. }
  27. /// <summary>
  28. /// 判断类型是否实现某个泛型
  29. /// </summary>
  30. /// <param name="type">类型</param>
  31. /// <param name="generic">泛型类型</param>
  32. /// <returns>bool</returns>
  33. public static bool HasImplementedRawGeneric(this Type type, Type generic)
  34. {
  35. // 检查接口类型
  36. var isTheRawGenericType = type.GetInterfaces().Any(IsTheRawGenericType);
  37. if (isTheRawGenericType) return true;
  38. // 检查类型
  39. while (type != null && type != typeof(object))
  40. {
  41. isTheRawGenericType = IsTheRawGenericType(type);
  42. if (isTheRawGenericType) return true;
  43. type = type.BaseType;
  44. }
  45. return false;
  46. // 判断逻辑
  47. bool IsTheRawGenericType(Type type) => generic == (type.IsGenericType ? type.GetGenericTypeDefinition() : type);
  48. }
  49. /// <summary>
  50. /// 将字典转化为QueryString格式
  51. /// </summary>
  52. /// <param name="dict"></param>
  53. /// <param name="urlEncode"></param>
  54. /// <returns></returns>
  55. public static string ToQueryString(this Dictionary<string, string> dict, bool urlEncode = true)
  56. {
  57. return string.Join("&", dict.Select(p => $"{(urlEncode ? p.Key?.UrlEncode() : "")}={(urlEncode ? p.Value?.UrlEncode() : "")}"));
  58. }
  59. /// <summary>
  60. /// 将字符串URL编码
  61. /// </summary>
  62. /// <param name="str"></param>
  63. /// <returns></returns>
  64. public static string UrlEncode(this string str)
  65. {
  66. return string.IsNullOrEmpty(str) ? "" : System.Web.HttpUtility.UrlEncode(str, Encoding.UTF8);
  67. }
  68. /// <summary>
  69. /// List转DataTable
  70. /// </summary>
  71. /// <typeparam name="T"></typeparam>
  72. /// <param name="list"></param>
  73. /// <returns></returns>
  74. public static DataTable ToDataTable<T>(this List<T> list)
  75. {
  76. DataTable result = new();
  77. if (list.Count > 0)
  78. {
  79. result.TableName = list[0].GetType().Name; // 表名赋值
  80. PropertyInfo[] propertys = list[0].GetType().GetProperties();
  81. foreach (PropertyInfo pi in propertys)
  82. {
  83. Type colType = pi.PropertyType;
  84. if (colType.IsGenericType && colType.GetGenericTypeDefinition() == typeof(Nullable<>))
  85. {
  86. colType = colType.GetGenericArguments()[0];
  87. }
  88. if (IsIgnoreColumn(pi))
  89. continue;
  90. result.Columns.Add(pi.Name, colType);
  91. }
  92. for (int i = 0; i < list.Count; i++)
  93. {
  94. ArrayList tempList = new();
  95. foreach (PropertyInfo pi in propertys)
  96. {
  97. if (IsIgnoreColumn(pi))
  98. continue;
  99. object obj = pi.GetValue(list[i], null);
  100. tempList.Add(obj);
  101. }
  102. object[] array = tempList.ToArray();
  103. result.LoadDataRow(array, true);
  104. }
  105. }
  106. return result;
  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. }
  119. }