public static bool DeepEquals(object obj1,object obj2)
{
// 如果两个栈中引用指向堆中的同一个实例,则一定相等
if (object.ReferenceEquals(obj1, obj2))
{
return true;
}
//将.NET对象转换成Json字符串,字符串相等,则.NET对象也相等
JToken t1 = JToken.FromObject(obj1);
JToken t2 = JToken.FromObject(obj2);
return JToken.DeepEquals(t1,t2 );
}
public new static bool ReferenceEquals(object obj1, object obj2)
{
return object.ReferenceEquals(obj1, obj2);
}
public static T DeepClone<T>(T obj)
{
//克隆Json字符串
JToken token = JToken.FromObject(obj);
token = token.DeepClone();
return token.ToObject<T>();
}
原文:https://www.cnblogs.com/browsepearls/p/13658565.html