ObjectExtension.cs 4.3 KB

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