From 404e1085a722dd6079fd97165778814566233ef1 Mon Sep 17 00:00:00 2001 From: falcon <9504402@qq.com> Date: Wed, 8 Mar 2023 15:56:59 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E4=BA=86=E4=B8=80=E4=B8=AA?= =?UTF-8?q?=E9=80=9A=E7=94=A8=E7=9A=84IEqualityComparer=E5=AE=9E?= =?UTF-8?q?=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Falcon.SugarApi/ObjectComparer.cs | 48 +++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 Falcon.SugarApi/ObjectComparer.cs diff --git a/Falcon.SugarApi/ObjectComparer.cs b/Falcon.SugarApi/ObjectComparer.cs new file mode 100644 index 0000000..b2aa265 --- /dev/null +++ b/Falcon.SugarApi/ObjectComparer.cs @@ -0,0 +1,48 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; + +namespace Falcon.SugarApi +{ + /// + /// 通用Object比较接口 + /// + /// 对象类型 + public class ObjectComparer:IEqualityComparer where T : class + { + /// + /// 比较两个对象是否相等 + /// + /// 对象1 + /// 对象2 + /// True相等,False不相等 + public virtual bool Equals(T? x,T? y) { + if(x == null && y == null) { + return true; + } + if(x == null || y == null) { + return false; + } + foreach(var p in typeof(T).GetProperties()) { + if(p.CanRead) { + var v1 = p.GetValue(x); + var v2 = p.GetValue(y); + if(v1!=v2) { + return false; + } + } + } + return true; + } + + /// + /// 获取对象HashCode + /// + /// 要获取的对象 + /// + /// + public int GetHashCode([DisallowNull] T obj) { + throw new NotImplementedException(); + } + } +}