新增了一个通用的IEqualityComparer<T>实现
This commit is contained in:
parent
c574f2241e
commit
404e1085a7
48
Falcon.SugarApi/ObjectComparer.cs
Normal file
48
Falcon.SugarApi/ObjectComparer.cs
Normal file
|
@ -0,0 +1,48 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
|
||||
namespace Falcon.SugarApi
|
||||
{
|
||||
/// <summary>
|
||||
/// 通用Object比较接口
|
||||
/// </summary>
|
||||
/// <typeparam name="T">对象类型</typeparam>
|
||||
public class ObjectComparer<T>:IEqualityComparer<T> where T : class
|
||||
{
|
||||
/// <summary>
|
||||
/// 比较两个对象是否相等
|
||||
/// </summary>
|
||||
/// <param name="x">对象1</param>
|
||||
/// <param name="y">对象2</param>
|
||||
/// <returns>True相等,False不相等</returns>
|
||||
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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取对象HashCode
|
||||
/// </summary>
|
||||
/// <param name="obj">要获取的对象</param>
|
||||
/// <returns></returns>
|
||||
/// <exception cref="NotImplementedException"></exception>
|
||||
public int GetHashCode([DisallowNull] T obj) {
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user