| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- 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
- {
- /// <summary>
- /// 扩展
- /// </summary>
- public static class ExtendUtil
- {
- #region 包含反转
- /// <summary>
- /// 包含反转
- /// </summary>
- /// <param name="value"></param>
- /// <param name="values"></param>
- /// <returns></returns>
- public static bool In(this object value, params object[] values) =>
- values.Contains(value);
- public static bool In<T>(this T value, params T[] values) where T : class =>
- values.Contains(value);
- #endregion
- /// <summary>
- /// 根据值得到中文备注
- /// </summary>
- /// <param name="t"></param>
- /// <param name="value"></param>
- /// <returns></returns>
- public static string GetEnumDesc<T>(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<T> AddRange<T>(this Collection<T> collection, IEnumerable<T> 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 深克隆
- /// <summary>
- /// Clones the specified list.
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="List">The list.</param>
- /// <returns>List{``0}.</returns>
- public static List<T> Clone<T>(this List<T> list) where T : class
- {
- var deserializeSettings = JsonSerializer.Serialize(list, new JsonSerializerOptions
- {
- Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping
- });
- return JsonSerializer.Deserialize<List<T>>(deserializeSettings);
- }
- #endregion
- }
- }
|