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;
namespace Masuit.Tools
{
public static class ObjectExtensions
{
private static readonly MethodInfo CloneMethod = typeof(object).GetMethod("MemberwiseClone", BindingFlags.NonPublic | BindingFlags.Instance);
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,
_ => false
};
}
public static object DeepClone(this object originalObject)
{
return InternalCopy(originalObject, new Dictionary