123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195 |
- using System;
- using System.Collections;
- using System.Diagnostics;
- using System.IO;
- using System.Linq;
- using System.Net;
- using System.Net.Sockets;
- using System.Numerics;
- using System.Text;
- using System.Text.RegularExpressions;
- using System.Threading.Tasks;
- using DnsClient;
- using Masuit.Tools.Strings;
- using Newtonsoft.Json;
- namespace Masuit.Tools
- {
- public static partial class ObjectExtensions
- {
- #region Map
- /// <summary>
- /// 浅克隆
- /// </summary>
- /// <param name="source">源</param>
- /// <typeparam name="TDestination">目标类型</typeparam>
- /// <returns>目标类型</returns>
- public static TDestination Clone<TDestination>(this object source)
- where TDestination : new()
- {
- TDestination dest = new TDestination();
- dest.GetType().GetProperties().ForEach(p =>
- {
- p.SetValue(dest, source.GetType().GetProperty(p.Name)?.GetValue(source));
- });
- return dest;
- }
- /// <summary>
- /// 深克隆
- /// </summary>
- /// <param name="source">源</param>
- /// <typeparam name="TDestination">目标类型</typeparam>
- /// <returns>目标类型</returns>
- public static TDestination DeepClone<TDestination>(this object source)
- where TDestination : new()
- {
- return JsonConvert.DeserializeObject<TDestination>(JsonConvert.SerializeObject(source));
- }
- /// <summary>
- /// 浅克隆(异步)
- /// </summary>
- /// <param name="source">源</param>
- /// <typeparam name="TDestination">目标类型</typeparam>
- /// <returns>目标类型</returns>
- public static Task<TDestination> CloneAsync<TDestination>(this object source)
- where TDestination : new()
- {
- return Task.Run(() =>
- {
- return source.Clone<TDestination>();
- });
- }
- /// <summary>
- /// 深克隆(异步)
- /// </summary>
- /// <param name="source">源</param>
- /// <typeparam name="TDestination">目标类型</typeparam>
- /// <returns>目标类型</returns>
- public static async Task<TDestination> DeepCloneAsync<TDestination>(this object source)
- where TDestination : new()
- {
- return await Task.Run(() => JsonConvert.DeserializeObject<TDestination>(JsonConvert.SerializeObject(source)));
- }
- #endregion Map
- #region CheckNull
- /// <summary>
- /// 检查参数是否为null,为null时抛出异常
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="obj"> 要检查的对象</param>
- /// <param name="paramName">抛出异常时,显示的参数名</param>
- /// <exception cref="ArgumentNullException"><paramref name="obj" /> 为null时抛出</exception>
- public static void CheckNullWithException<T>(this T obj, string paramName)
- {
- if (obj == null) throw new ArgumentNullException(paramName);
- }
- /// <summary>
- /// 检查参数是否为null,为null时抛出异常
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="obj"> 要检查的对象</param>
- /// <param name="paramName">抛出异常时,显示的参数名</param>
- /// <param name="message"> 抛出异常时,显示的错误信息</param>
- /// <exception cref="ArgumentNullException"><paramref name="obj" /> 为null时抛出</exception>
- public static void CheckNullWithException<T>(this T obj, string paramName, string message)
- {
- if (obj == null) throw new ArgumentNullException(paramName, message);
- }
- /// <summary>
- /// 检查参数是否为null或emtpy,为null或emtpy时抛出异常
- /// </summary>
- /// <param name="obj"> 要检查的对象</param>
- /// <param name="paramName">抛出异常时,显示的参数名</param>
- /// <exception cref="ArgumentNullException"><paramref name="obj" /> 为null或emtpy时抛出</exception>
- public static void CheckNullOrEmptyWithException(this IEnumerable obj, string paramName)
- {
- if (obj.IsNullOrEmpty()) throw new ArgumentNullException(paramName);
- }
- /// <summary>
- /// 检查参数是否为null或emtpy,为null或emtpy时抛出异常
- /// </summary>
- /// <param name="obj"> 要检查的对象</param>
- /// <param name="paramName">抛出异常时,显示的参数名</param>
- /// <param name="message"> 抛出异常时,显示的错误信息</param>
- /// <exception cref="ArgumentNullException"><paramref name="obj" /> 为null或emtpy时抛出</exception>
- public static void CheckNullOrEmptyWithException(this IEnumerable obj, string paramName, string message)
- {
- if (obj.IsNullOrEmpty()) throw new ArgumentNullException(paramName, message);
- }
- #endregion CheckNull
- /// <summary>
- /// 判断是否为null,null或0长度都返回true
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="value"></param>
- /// <returns></returns>
- public static bool IsNullOrEmpty<T>(this T value)
- where T : class?
- {
- #region 1.对象级别
- //引用为null
- bool isObjectNull = value == null;
- if (isObjectNull == true) return true;
- //判断是否为集合
- IEnumerator? tempEnumerator = (value as IEnumerable)?.GetEnumerator();
- if (tempEnumerator == null) return false;//这里出去代表是对象 且 引用不为null.所以为false
- #endregion 1.对象级别
- #region 2.集合级别
- //到这里就代表是集合且引用不为空,判断长度
- //MoveNext方法返回tue代表集合中至少有一个数据,返回false就代表0长度
- bool isZeroLenth = tempEnumerator.MoveNext() == false;
- if (isZeroLenth == true) return true;
- return isZeroLenth;
- #endregion 2.集合级别
- }
- /// <summary>
- /// 转换成json字符串
- /// </summary>
- /// <param name="source"></param>
- /// <returns></returns>
- public static string ToJsonExt<T>(this T obj, JsonSerializerSettings? setting = null)
- {
- if (obj == null) return string.Empty;
- if (setting == null)
- {
- return JsonConvert.SerializeObject(obj);
- }
- else
- {
- return JsonConvert.SerializeObject(obj, setting);
- }
- }
- /// <summary>
- /// 严格比较两个对象是否是同一对象(判断引用)
- /// </summary>
- /// <param name="this">自己</param>
- /// <param name="o">需要比较的对象</param>
- /// <returns>是否同一对象</returns>
- public new static bool ReferenceEquals(this object @this, object o)
- {
- return object.ReferenceEquals(@this, o);
- }
- }
- }
|