ObjectExtension.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. namespace Admin.NET.Core;
  2. /// <summary>
  3. /// 对象拓展
  4. /// </summary>
  5. [SuppressSniffer]
  6. public static partial 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. /// <summary>
  119. /// 将object转换为long,若失败则返回0
  120. /// </summary>
  121. /// <param name="obj"></param>
  122. /// <returns></returns>
  123. public static long ParseToLong(this object obj)
  124. {
  125. try
  126. {
  127. return long.Parse(obj.ToString());
  128. }
  129. catch
  130. {
  131. return 0L;
  132. }
  133. }
  134. /// <summary>
  135. /// 将object转换为long,若失败则返回指定值
  136. /// </summary>
  137. /// <param name="str"></param>
  138. /// <param name="defaultValue"></param>
  139. /// <returns></returns>
  140. public static long ParseToLong(this string str, long defaultValue)
  141. {
  142. try
  143. {
  144. return long.Parse(str);
  145. }
  146. catch
  147. {
  148. return defaultValue;
  149. }
  150. }
  151. /// <summary>
  152. /// 将object转换为double,若失败则返回0
  153. /// </summary>
  154. /// <param name="obj"></param>
  155. /// <returns></returns>
  156. public static double ParseToDouble(this object obj)
  157. {
  158. try
  159. {
  160. return double.Parse(obj.ToString());
  161. }
  162. catch
  163. {
  164. return 0;
  165. }
  166. }
  167. /// <summary>
  168. /// 将object转换为double,若失败则返回指定值
  169. /// </summary>
  170. /// <param name="str"></param>
  171. /// <param name="defaultValue"></param>
  172. /// <returns></returns>
  173. public static double ParseToDouble(this object str, double defaultValue)
  174. {
  175. try
  176. {
  177. return double.Parse(str.ToString());
  178. }
  179. catch
  180. {
  181. return defaultValue;
  182. }
  183. }
  184. /// <summary>
  185. /// 将string转换为DateTime,若失败则返回日期最小值
  186. /// </summary>
  187. /// <param name="str"></param>
  188. /// <returns></returns>
  189. public static DateTime ParseToDateTime(this string str)
  190. {
  191. try
  192. {
  193. if (string.IsNullOrWhiteSpace(str))
  194. {
  195. return DateTime.MinValue;
  196. }
  197. if (str.Contains("-") || str.Contains("/"))
  198. {
  199. return DateTime.Parse(str);
  200. }
  201. else
  202. {
  203. int length = str.Length;
  204. switch (length)
  205. {
  206. case 4:
  207. return DateTime.ParseExact(str, "yyyy", System.Globalization.CultureInfo.CurrentCulture);
  208. case 6:
  209. return DateTime.ParseExact(str, "yyyyMM", System.Globalization.CultureInfo.CurrentCulture);
  210. case 8:
  211. return DateTime.ParseExact(str, "yyyyMMdd", System.Globalization.CultureInfo.CurrentCulture);
  212. case 10:
  213. return DateTime.ParseExact(str, "yyyyMMddHH", System.Globalization.CultureInfo.CurrentCulture);
  214. case 12:
  215. return DateTime.ParseExact(str, "yyyyMMddHHmm", System.Globalization.CultureInfo.CurrentCulture);
  216. case 14:
  217. return DateTime.ParseExact(str, "yyyyMMddHHmmss", System.Globalization.CultureInfo.CurrentCulture);
  218. default:
  219. return DateTime.ParseExact(str, "yyyyMMddHHmmss", System.Globalization.CultureInfo.CurrentCulture);
  220. }
  221. }
  222. }
  223. catch
  224. {
  225. return DateTime.MinValue;
  226. }
  227. }
  228. /// <summary>
  229. /// 将string转换为DateTime,若失败则返回默认值
  230. /// </summary>
  231. /// <param name="str"></param>
  232. /// <param name="defaultValue"></param>
  233. /// <returns></returns>
  234. public static DateTime ParseToDateTime(this string str, DateTime? defaultValue)
  235. {
  236. try
  237. {
  238. if (string.IsNullOrWhiteSpace(str))
  239. {
  240. return defaultValue.GetValueOrDefault();
  241. }
  242. if (str.Contains("-") || str.Contains("/"))
  243. {
  244. return DateTime.Parse(str);
  245. }
  246. else
  247. {
  248. int length = str.Length;
  249. switch (length)
  250. {
  251. case 4:
  252. return DateTime.ParseExact(str, "yyyy", System.Globalization.CultureInfo.CurrentCulture);
  253. case 6:
  254. return DateTime.ParseExact(str, "yyyyMM", System.Globalization.CultureInfo.CurrentCulture);
  255. case 8:
  256. return DateTime.ParseExact(str, "yyyyMMdd", System.Globalization.CultureInfo.CurrentCulture);
  257. case 10:
  258. return DateTime.ParseExact(str, "yyyyMMddHH", System.Globalization.CultureInfo.CurrentCulture);
  259. case 12:
  260. return DateTime.ParseExact(str, "yyyyMMddHHmm", System.Globalization.CultureInfo.CurrentCulture);
  261. case 14:
  262. return DateTime.ParseExact(str, "yyyyMMddHHmmss", System.Globalization.CultureInfo.CurrentCulture);
  263. default:
  264. return DateTime.ParseExact(str, "yyyyMMddHHmmss", System.Globalization.CultureInfo.CurrentCulture);
  265. }
  266. }
  267. }
  268. catch
  269. {
  270. return defaultValue.GetValueOrDefault();
  271. }
  272. }
  273. }