EasyMES/WaterCloud.Code/Extend/ExtList.Comparint.cs
2022-10-20 17:12:54 +08:00

51 lines
1.8 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*******************************************************************************
* Copyright © 2016 WaterCloud.Framework 版权所有
* Author: WaterCloud
* Description: WaterCloud快速开发平台
* Website
*********************************************************************************/
using System.Collections.Generic;
using System.Linq;
namespace WaterCloud.Code
{
public class ExtList<T> : IEqualityComparer<T> where T : class, new()
{
private string[] comparintFiledName = { };
public ExtList() { }
public ExtList(params string[] comparintFiledName)
{
this.comparintFiledName = comparintFiledName;
}
bool IEqualityComparer<T>.Equals(T x, T y)
{
if (x == null && y == null)
{
return false;
}
if (comparintFiledName.Length == 0)
{
return x.Equals(y);
}
bool result = true;
var typeX = x.GetType();//获取类型
var typeY = y.GetType();
foreach (var filedName in comparintFiledName)
{
var xPropertyInfo = (from p in typeX.GetProperties() where p.Name.Equals(filedName) select p).FirstOrDefault();
var yPropertyInfo = (from p in typeY.GetProperties() where p.Name.Equals(filedName) select p).FirstOrDefault();
result = result
&& xPropertyInfo != null && yPropertyInfo != null
&& xPropertyInfo.GetValue(x, null).ToString().Equals(yPropertyInfo.GetValue(y, null));
}
return result;
}
int IEqualityComparer<T>.GetHashCode(T obj)
{
return obj.ToString().GetHashCode();
}
}
}