ObjectExtensions.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. using System;
  2. using System.Collections;
  3. using System.Diagnostics;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Net;
  7. using System.Net.Sockets;
  8. using System.Numerics;
  9. using System.Text;
  10. using System.Text.RegularExpressions;
  11. using System.Threading.Tasks;
  12. using DnsClient;
  13. using Masuit.Tools.Strings;
  14. using Newtonsoft.Json;
  15. namespace Masuit.Tools
  16. {
  17. public static partial class ObjectExtensions
  18. {
  19. #region Map
  20. /// <summary>
  21. /// 浅克隆
  22. /// </summary>
  23. /// <param name="source">源</param>
  24. /// <typeparam name="TDestination">目标类型</typeparam>
  25. /// <returns>目标类型</returns>
  26. public static TDestination Clone<TDestination>(this object source)
  27. where TDestination : new()
  28. {
  29. TDestination dest = new TDestination();
  30. dest.GetType().GetProperties().ForEach(p =>
  31. {
  32. p.SetValue(dest, source.GetType().GetProperty(p.Name)?.GetValue(source));
  33. });
  34. return dest;
  35. }
  36. /// <summary>
  37. /// 深克隆
  38. /// </summary>
  39. /// <param name="source">源</param>
  40. /// <typeparam name="TDestination">目标类型</typeparam>
  41. /// <returns>目标类型</returns>
  42. public static TDestination DeepClone<TDestination>(this object source)
  43. where TDestination : new()
  44. {
  45. return JsonConvert.DeserializeObject<TDestination>(JsonConvert.SerializeObject(source));
  46. }
  47. /// <summary>
  48. /// 浅克隆(异步)
  49. /// </summary>
  50. /// <param name="source">源</param>
  51. /// <typeparam name="TDestination">目标类型</typeparam>
  52. /// <returns>目标类型</returns>
  53. public static Task<TDestination> CloneAsync<TDestination>(this object source)
  54. where TDestination : new()
  55. {
  56. return Task.Run(() =>
  57. {
  58. return source.Clone<TDestination>();
  59. });
  60. }
  61. /// <summary>
  62. /// 深克隆(异步)
  63. /// </summary>
  64. /// <param name="source">源</param>
  65. /// <typeparam name="TDestination">目标类型</typeparam>
  66. /// <returns>目标类型</returns>
  67. public static async Task<TDestination> DeepCloneAsync<TDestination>(this object source)
  68. where TDestination : new()
  69. {
  70. return await Task.Run(() => JsonConvert.DeserializeObject<TDestination>(JsonConvert.SerializeObject(source)));
  71. }
  72. #endregion Map
  73. #region CheckNull
  74. /// <summary>
  75. /// 检查参数是否为null,为null时抛出异常
  76. /// </summary>
  77. /// <typeparam name="T"></typeparam>
  78. /// <param name="obj"> 要检查的对象</param>
  79. /// <param name="paramName">抛出异常时,显示的参数名</param>
  80. /// <exception cref="ArgumentNullException"><paramref name="obj" /> 为null时抛出</exception>
  81. public static void CheckNullWithException<T>(this T obj, string paramName)
  82. {
  83. if (obj == null) throw new ArgumentNullException(paramName);
  84. }
  85. /// <summary>
  86. /// 检查参数是否为null,为null时抛出异常
  87. /// </summary>
  88. /// <typeparam name="T"></typeparam>
  89. /// <param name="obj"> 要检查的对象</param>
  90. /// <param name="paramName">抛出异常时,显示的参数名</param>
  91. /// <param name="message"> 抛出异常时,显示的错误信息</param>
  92. /// <exception cref="ArgumentNullException"><paramref name="obj" /> 为null时抛出</exception>
  93. public static void CheckNullWithException<T>(this T obj, string paramName, string message)
  94. {
  95. if (obj == null) throw new ArgumentNullException(paramName, message);
  96. }
  97. /// <summary>
  98. /// 检查参数是否为null或emtpy,为null或emtpy时抛出异常
  99. /// </summary>
  100. /// <param name="obj"> 要检查的对象</param>
  101. /// <param name="paramName">抛出异常时,显示的参数名</param>
  102. /// <exception cref="ArgumentNullException"><paramref name="obj" /> 为null或emtpy时抛出</exception>
  103. public static void CheckNullOrEmptyWithException(this IEnumerable obj, string paramName)
  104. {
  105. if (obj.IsNullOrEmpty()) throw new ArgumentNullException(paramName);
  106. }
  107. /// <summary>
  108. /// 检查参数是否为null或emtpy,为null或emtpy时抛出异常
  109. /// </summary>
  110. /// <param name="obj"> 要检查的对象</param>
  111. /// <param name="paramName">抛出异常时,显示的参数名</param>
  112. /// <param name="message"> 抛出异常时,显示的错误信息</param>
  113. /// <exception cref="ArgumentNullException"><paramref name="obj" /> 为null或emtpy时抛出</exception>
  114. public static void CheckNullOrEmptyWithException(this IEnumerable obj, string paramName, string message)
  115. {
  116. if (obj.IsNullOrEmpty()) throw new ArgumentNullException(paramName, message);
  117. }
  118. #endregion CheckNull
  119. /// <summary>
  120. /// 判断是否为null,null或0长度都返回true
  121. /// </summary>
  122. /// <typeparam name="T"></typeparam>
  123. /// <param name="value"></param>
  124. /// <returns></returns>
  125. public static bool IsNullOrEmpty<T>(this T value)
  126. where T : class?
  127. {
  128. #region 1.对象级别
  129. //引用为null
  130. bool isObjectNull = value == null;
  131. if (isObjectNull == true) return true;
  132. //判断是否为集合
  133. IEnumerator? tempEnumerator = (value as IEnumerable)?.GetEnumerator();
  134. if (tempEnumerator == null) return false;//这里出去代表是对象 且 引用不为null.所以为false
  135. #endregion 1.对象级别
  136. #region 2.集合级别
  137. //到这里就代表是集合且引用不为空,判断长度
  138. //MoveNext方法返回tue代表集合中至少有一个数据,返回false就代表0长度
  139. bool isZeroLenth = tempEnumerator.MoveNext() == false;
  140. if (isZeroLenth == true) return true;
  141. return isZeroLenth;
  142. #endregion 2.集合级别
  143. }
  144. /// <summary>
  145. /// 转换成json字符串
  146. /// </summary>
  147. /// <param name="source"></param>
  148. /// <returns></returns>
  149. public static string ToJsonExt<T>(this T obj, JsonSerializerSettings? setting = null)
  150. {
  151. if (obj == null) return string.Empty;
  152. if (setting == null)
  153. {
  154. return JsonConvert.SerializeObject(obj);
  155. }
  156. else
  157. {
  158. return JsonConvert.SerializeObject(obj, setting);
  159. }
  160. }
  161. /// <summary>
  162. /// 严格比较两个对象是否是同一对象(判断引用)
  163. /// </summary>
  164. /// <param name="this">自己</param>
  165. /// <param name="o">需要比较的对象</param>
  166. /// <returns>是否同一对象</returns>
  167. public new static bool ReferenceEquals(this object @this, object o)
  168. {
  169. return object.ReferenceEquals(@this, o);
  170. }
  171. }
  172. }