using Newtonsoft.Json;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Masuit.Tools.Dynamics;
using Masuit.Tools.Reflection;
using Newtonsoft.Json.Linq;
#if NETSTANDARD2_1_OR_GREATER
using System.Text.Json;
using Masuit.Tools.Systems;
using JsonSerializer = System.Text.Json.JsonSerializer;
#endif
#if NET5_0_OR_GREATER
using System.Text.Json;
using Masuit.Tools.Systems;
using JsonSerializer = System.Text.Json.JsonSerializer;
using System.Text.Encodings.Web;
using System.Text.Json.Serialization;
#endif
namespace Masuit.Tools;
///
/// 对象扩展
///
public static class ObjectExtensions
{
private static readonly MethodInfo CloneMethod = typeof(object).GetMethod("MemberwiseClone", BindingFlags.NonPublic | BindingFlags.Instance);
#if NET5_0_OR_GREATER
///
/// System.Text.Json 默认配置 支持中文
///
private static readonly JsonSerializerOptions DefaultJsonSerializerOptions = new()
{
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping,
};
///
/// System.Text.Json 支持中文/忽略null值
///
private static readonly JsonSerializerOptions IgnoreNullJsonSerializerOptions = new()
{
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping,
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull
};
#endif
///
/// 是否是基本数据类型
///
///
///
public static bool IsPrimitive(this Type type)
{
if (type == typeof(string))
{
return true;
}
return type.IsValueType && type.IsPrimitive;
}
///
/// 判断类型是否是常见的简单类型
///
///
///
public static bool IsSimpleType(this Type type)
{
//IsPrimitive 判断是否为基础类型。
//基元类型为 Boolean、 Byte、 SByte、 Int16、 UInt16、 Int32、 UInt32、 Int64、 UInt64、 IntPtr、 UIntPtr、 Char、 Double 和 Single。
var t = Nullable.GetUnderlyingType(type) ?? type;
return t.IsPrimitive || t.IsEnum || t == typeof(decimal) || t == typeof(string) || t == typeof(Guid) || t == typeof(TimeSpan) || t == typeof(Uri);
}
///
/// 是否是常见类型的 数组形式 类型
///
///
///
public static bool IsSimpleArrayType(this Type type)
{
return type.IsArray && Type.GetType(type.FullName!.Trim('[', ']')).IsSimpleType();
}
///
/// 是否是常见类型的 泛型形式 类型
///
///
///
public static bool IsSimpleListType(this Type type)
{
type = Nullable.GetUnderlyingType(type) ?? type;
return type.IsGenericType && type.GetGenericArguments().Length == 1 && type.GetGenericArguments().FirstOrDefault().IsSimpleType();
}
///
/// 是否是默认值
///
///
///
public static bool IsDefaultValue(this object value)
{
if (value == default)
{
return true;
}
return value switch
{
byte s => s == 0,
sbyte s => s == 0,
short s => s == 0,
char s => s == 0,
bool s => s == false,
ushort s => s == 0,
int s => s == 0,
uint s => s == 0,
long s => s == 0,
ulong s => s == 0,
decimal s => s == 0,
float s => s == 0,
double s => s == 0,
Enum s => Equals(s, Enum.GetValues(value.GetType()).GetValue(0)),
DateTime s => s == DateTime.MinValue,
DateTimeOffset s => s == DateTimeOffset.MinValue,
Guid g => g == Guid.Empty,
ValueType => Activator.CreateInstance(value.GetType()).Equals(value),
_ => false
};
}
///
/// 深克隆
///
///
/// 使用json方式
///
public static object DeepClone(this object originalObject, bool useJson = false)
{
return useJson
? InternalJsonCopy(originalObject)
: InternalCopy(originalObject, new Dictionary