using System; using System.Linq; using System.Text.Json; using System.Reflection; using System.ComponentModel; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Text.Encodings.Web; namespace SHJX.Service.Common.Extend { /// /// 扩展 /// public static class ExtendUtil { #region 包含反转 /// /// 包含反转 /// /// /// /// public static bool In(this object value, params object[] values) => values.Contains(value); public static bool In(this T value, params T[] values) where T : class => values.Contains(value); #endregion /// /// 根据值得到中文备注 /// /// /// /// public static string GetEnumDesc(this Enum value) where T : Enum { Type t = typeof(T); FieldInfo[] fields = t.GetFields(); for (int i = 1, count = fields.Length; i < count; i++) { if (Enum.Parse(t, fields[i].Name) != value) continue; var enumAttributes = (DescriptionAttribute[])fields[i].GetCustomAttributes(typeof(DescriptionAttribute), false); if (enumAttributes.Length > 0) { return enumAttributes[0].Description; } } return string.Empty; } public static string Uuid => Guid.NewGuid().ToString("N").ToUpper(); public static Collection AddRange(this Collection collection, IEnumerable items) { if (collection == null) throw new ArgumentNullException(nameof (collection)); if (items == null) throw new ArgumentNullException(nameof (items)); foreach (T obj in items) collection.Add(obj); return collection; } #region 深克隆 /// /// Clones the specified list. /// /// /// The list. /// List{``0}. public static List Clone(this List list) where T : class { var deserializeSettings = JsonSerializer.Serialize(list, new JsonSerializerOptions { Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping }); return JsonSerializer.Deserialize>(deserializeSettings); } #endregion } }