ObjectExtensions.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. using Newtonsoft.Json;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Reflection;
  7. using Masuit.Tools.Dynamics;
  8. using Masuit.Tools.Reflection;
  9. using Newtonsoft.Json.Linq;
  10. namespace Masuit.Tools
  11. {
  12. public static class ObjectExtensions
  13. {
  14. private static readonly MethodInfo CloneMethod = typeof(object).GetMethod("MemberwiseClone", BindingFlags.NonPublic | BindingFlags.Instance);
  15. public static bool IsPrimitive(this Type type)
  16. {
  17. if (type == typeof(string))
  18. {
  19. return true;
  20. }
  21. return type.IsValueType & type.IsPrimitive;
  22. }
  23. public static bool IsDefaultValue(this object value)
  24. {
  25. if (value == default)
  26. {
  27. return true;
  28. }
  29. return value switch
  30. {
  31. byte s => s == 0,
  32. sbyte s => s == 0,
  33. short s => s == 0,
  34. char s => s == 0,
  35. bool s => s == false,
  36. ushort s => s == 0,
  37. int s => s == 0,
  38. uint s => s == 0,
  39. long s => s == 0,
  40. ulong s => s == 0,
  41. decimal s => s == 0,
  42. float s => s == 0,
  43. double s => s == 0,
  44. Enum s => Equals(s, Enum.GetValues(value.GetType()).GetValue(0)),
  45. DateTime s => s == DateTime.MinValue,
  46. DateTimeOffset s => s == DateTimeOffset.MinValue,
  47. _ => false
  48. };
  49. }
  50. public static object DeepClone(this object originalObject)
  51. {
  52. return InternalCopy(originalObject, new Dictionary<object, object>(new ReferenceEqualityComparer()));
  53. }
  54. public static T DeepClone<T>(this T original)
  55. {
  56. return (T)DeepClone((object)original);
  57. }
  58. private static object InternalCopy(object originalObject, IDictionary<object, object> visited)
  59. {
  60. if (originalObject == null)
  61. {
  62. return null;
  63. }
  64. var typeToReflect = originalObject.GetType();
  65. if (IsPrimitive(typeToReflect))
  66. {
  67. return originalObject;
  68. }
  69. if (visited.ContainsKey(originalObject))
  70. {
  71. return visited[originalObject];
  72. }
  73. if (typeof(Delegate).IsAssignableFrom(typeToReflect))
  74. {
  75. return null;
  76. }
  77. var cloneObject = CloneMethod.Invoke(originalObject, null);
  78. if (typeToReflect.IsArray)
  79. {
  80. var arrayType = typeToReflect.GetElementType();
  81. if (!IsPrimitive(arrayType))
  82. {
  83. Array clonedArray = (Array)cloneObject;
  84. clonedArray.ForEach((array, indices) => array.SetValue(InternalCopy(clonedArray.GetValue(indices), visited), indices));
  85. }
  86. }
  87. visited.Add(originalObject, cloneObject);
  88. CopyFields(originalObject, visited, cloneObject, typeToReflect);
  89. RecursiveCopyBaseTypePrivateFields(originalObject, visited, cloneObject, typeToReflect);
  90. return cloneObject;
  91. }
  92. private static void RecursiveCopyBaseTypePrivateFields(object originalObject, IDictionary<object, object> visited, object cloneObject, Type typeToReflect)
  93. {
  94. if (typeToReflect.BaseType != null)
  95. {
  96. RecursiveCopyBaseTypePrivateFields(originalObject, visited, cloneObject, typeToReflect.BaseType);
  97. CopyFields(originalObject, visited, cloneObject, typeToReflect.BaseType, BindingFlags.Instance | BindingFlags.NonPublic, info => info.IsPrivate);
  98. }
  99. }
  100. private static void CopyFields(object originalObject, IDictionary<object, object> visited, object cloneObject, Type typeToReflect, BindingFlags bindingFlags = BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.FlattenHierarchy, Func<FieldInfo, bool> filter = null)
  101. {
  102. foreach (FieldInfo fieldInfo in typeToReflect.GetFields(bindingFlags))
  103. {
  104. if (filter != null && !filter(fieldInfo))
  105. {
  106. continue;
  107. }
  108. if (IsPrimitive(fieldInfo.FieldType) || fieldInfo.IsInitOnly)
  109. {
  110. continue;
  111. }
  112. var originalFieldValue = fieldInfo.GetValue(originalObject);
  113. var clonedFieldValue = InternalCopy(originalFieldValue, visited);
  114. fieldInfo.SetValue(cloneObject, clonedFieldValue);
  115. }
  116. }
  117. /// <summary>
  118. /// 判断是否为null,null或0长度都返回true
  119. /// </summary>
  120. /// <typeparam name="T"></typeparam>
  121. /// <param name="value"></param>
  122. /// <returns></returns>
  123. public static bool IsNullOrEmpty<T>(this T value)
  124. where T : class
  125. {
  126. #region 1.对象级别
  127. //引用为null
  128. bool isObjectNull = value is null;
  129. if (isObjectNull)
  130. {
  131. return true;
  132. }
  133. //判断是否为集合
  134. var tempEnumerator = (value as IEnumerable)?.GetEnumerator();
  135. if (tempEnumerator == null) return false;//这里出去代表是对象 且 引用不为null.所以为false
  136. #endregion 1.对象级别
  137. #region 2.集合级别
  138. //到这里就代表是集合且引用不为空,判断长度
  139. //MoveNext方法返回tue代表集合中至少有一个数据,返回false就代表0长度
  140. bool isZeroLenth = tempEnumerator.MoveNext() == false;
  141. if (isZeroLenth) return true;
  142. return isZeroLenth;
  143. #endregion 2.集合级别
  144. }
  145. /// <summary>
  146. /// 转成非null
  147. /// </summary>
  148. /// <param name="s"></param>
  149. /// <param name="value">为空时的替换值</param>
  150. /// <returns></returns>
  151. public static T IfNull<T>(this T s, in T value)
  152. {
  153. return s ?? value;
  154. }
  155. /// <summary>
  156. /// 转换成json字符串
  157. /// </summary>
  158. /// <param name="obj"></param>
  159. /// <param name="setting"></param>
  160. /// <returns></returns>
  161. public static string ToJsonString(this object obj, JsonSerializerSettings setting = null)
  162. {
  163. if (obj == null) return string.Empty;
  164. return JsonConvert.SerializeObject(obj, setting);
  165. }
  166. /// <summary>
  167. /// 链式操作
  168. /// </summary>
  169. /// <typeparam name="T1"></typeparam>
  170. /// <typeparam name="T2"></typeparam>
  171. /// <param name="source"></param>
  172. /// <param name="action"></param>
  173. public static T2 Next<T1, T2>(this T1 source, Func<T1, T2> action)
  174. {
  175. return action(source);
  176. }
  177. /// <summary>
  178. /// 将对象转换成字典
  179. /// </summary>
  180. /// <param name="value"></param>
  181. public static Dictionary<string, object> ToDictionary(this object value)
  182. {
  183. var dictionary = new Dictionary<string, object>();
  184. if (value != null)
  185. {
  186. foreach (var property in value.GetType().GetProperties())
  187. {
  188. var obj = property.GetValue(value, null);
  189. dictionary.Add(property.Name, obj);
  190. }
  191. }
  192. return dictionary;
  193. }
  194. /// <summary>
  195. /// 将对象转换成字典
  196. /// </summary>
  197. /// <param name="value"></param>
  198. public static Dictionary<string, string> ToDictionary(this JObject value)
  199. {
  200. var dictionary = new Dictionary<string, string>();
  201. if (value != null)
  202. {
  203. var enumerator = value.GetEnumerator();
  204. while (enumerator.MoveNext())
  205. {
  206. var obj = enumerator.Current.Value ?? string.Empty;
  207. dictionary.Add(enumerator.Current.Key, obj + string.Empty);
  208. }
  209. }
  210. return dictionary;
  211. }
  212. /// <summary>
  213. /// 对象转换成动态类型
  214. /// </summary>
  215. /// <param name="obj"></param>
  216. /// <returns></returns>
  217. public static dynamic ToDynamic(this object obj)
  218. {
  219. return DynamicFactory.WithObject(obj);
  220. }
  221. /// <summary>
  222. /// 多个对象的属性值合并
  223. /// </summary>
  224. /// <typeparam name="T"></typeparam>
  225. /// <param name="a"></param>
  226. /// <param name="b"></param>
  227. /// <param name="others"></param>
  228. public static T Merge<T>(this T a, T b, params T[] others) where T : class
  229. {
  230. foreach (var item in new[] { b }.Concat(others))
  231. {
  232. var dic = item.ToDictionary();
  233. foreach (var p in dic.Where(p => a.GetProperty(p.Key).IsDefaultValue()))
  234. {
  235. a.SetProperty(p.Key, p.Value);
  236. }
  237. }
  238. return a;
  239. }
  240. /// <summary>
  241. /// 多个对象的属性值合并
  242. /// </summary>
  243. /// <typeparam name="T"></typeparam>
  244. public static T Merge<T>(this IEnumerable<T> objects) where T : class
  245. {
  246. var list = objects as List<T> ?? objects.ToList();
  247. if (list.Count == 0)
  248. {
  249. return null;
  250. }
  251. if (list.Count == 1)
  252. {
  253. return list[0];
  254. }
  255. foreach (var item in list.Skip(1))
  256. {
  257. var dic = item.ToDictionary();
  258. foreach (var p in dic.Where(p => list[0].GetProperty(p.Key).IsDefaultValue()))
  259. {
  260. list[0].SetProperty(p.Key, p.Value);
  261. }
  262. }
  263. return list[0];
  264. }
  265. }
  266. internal class ReferenceEqualityComparer : EqualityComparer<object>
  267. {
  268. public override bool Equals(object x, object y)
  269. {
  270. return ReferenceEquals(x, y);
  271. }
  272. public override int GetHashCode(object obj)
  273. {
  274. if (obj is null) return 0;
  275. return obj.GetHashCode();
  276. }
  277. }
  278. internal static class ArrayExtensions
  279. {
  280. public static void ForEach(this Array array, Action<Array, int[]> action)
  281. {
  282. if (array.LongLength == 0)
  283. {
  284. return;
  285. }
  286. ArrayTraverse walker = new ArrayTraverse(array);
  287. do action(array, walker.Position);
  288. while (walker.Step());
  289. }
  290. internal class ArrayTraverse
  291. {
  292. public int[] Position;
  293. private readonly int[] _maxLengths;
  294. public ArrayTraverse(Array array)
  295. {
  296. _maxLengths = new int[array.Rank];
  297. for (int i = 0; i < array.Rank; ++i)
  298. {
  299. _maxLengths[i] = array.GetLength(i) - 1;
  300. }
  301. Position = new int[array.Rank];
  302. }
  303. public bool Step()
  304. {
  305. for (int i = 0; i < Position.Length; ++i)
  306. {
  307. if (Position[i] < _maxLengths[i])
  308. {
  309. Position[i]++;
  310. for (int j = 0; j < i; j++)
  311. {
  312. Position[j] = 0;
  313. }
  314. return true;
  315. }
  316. }
  317. return false;
  318. }
  319. }
  320. }
  321. }