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
///
/// 浅克隆
///
/// 源
/// 目标类型
/// 目标类型
public static TDestination Clone(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;
}
///
/// 深克隆
///
/// 源
/// 目标类型
/// 目标类型
public static TDestination DeepClone(this object source)
where TDestination : new()
{
return JsonConvert.DeserializeObject(JsonConvert.SerializeObject(source));
}
///
/// 浅克隆(异步)
///
/// 源
/// 目标类型
/// 目标类型
public static Task CloneAsync(this object source)
where TDestination : new()
{
return Task.Run(() =>
{
return source.Clone();
});
}
///
/// 深克隆(异步)
///
/// 源
/// 目标类型
/// 目标类型
public static async Task DeepCloneAsync(this object source)
where TDestination : new()
{
return await Task.Run(() => JsonConvert.DeserializeObject(JsonConvert.SerializeObject(source)));
}
#endregion Map
#region CheckNull
///
/// 检查参数是否为null,为null时抛出异常
///
///
/// 要检查的对象
/// 抛出异常时,显示的参数名
/// 为null时抛出
public static void CheckNullWithException(this T obj, string paramName)
{
if (obj == null) throw new ArgumentNullException(paramName);
}
///
/// 检查参数是否为null,为null时抛出异常
///
///
/// 要检查的对象
/// 抛出异常时,显示的参数名
/// 抛出异常时,显示的错误信息
/// 为null时抛出
public static void CheckNullWithException(this T obj, string paramName, string message)
{
if (obj == null) throw new ArgumentNullException(paramName, message);
}
///
/// 检查参数是否为null或emtpy,为null或emtpy时抛出异常
///
/// 要检查的对象
/// 抛出异常时,显示的参数名
/// 为null或emtpy时抛出
public static void CheckNullOrEmptyWithException(this IEnumerable obj, string paramName)
{
if (obj.IsNullOrEmpty()) throw new ArgumentNullException(paramName);
}
///
/// 检查参数是否为null或emtpy,为null或emtpy时抛出异常
///
/// 要检查的对象
/// 抛出异常时,显示的参数名
/// 抛出异常时,显示的错误信息
/// 为null或emtpy时抛出
public static void CheckNullOrEmptyWithException(this IEnumerable obj, string paramName, string message)
{
if (obj.IsNullOrEmpty()) throw new ArgumentNullException(paramName, message);
}
#endregion CheckNull
///
/// 判断是否为null,null或0长度都返回true
///
///
///
///
public static bool IsNullOrEmpty(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.集合级别
}
///
/// 转换成json字符串
///
///
///
public static string ToJsonExt(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);
}
}
///
/// 严格比较两个对象是否是同一对象(判断引用)
///
/// 自己
/// 需要比较的对象
/// 是否同一对象
public new static bool ReferenceEquals(this object @this, object o)
{
return object.ReferenceEquals(@this, o);
}
}
}