纽威
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

44 lines
1.5 KiB

3 years ago
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. namespace NFine.Code
  4. {
  5. public class ExtList<T> : IEqualityComparer<T> where T : class, new()
  6. {
  7. private string[] comparintFiledName = { };
  8. public ExtList() { }
  9. public ExtList(params string[] comparintFiledName)
  10. {
  11. this.comparintFiledName = comparintFiledName;
  12. }
  13. bool IEqualityComparer<T>.Equals(T x, T y)
  14. {
  15. if (x == null && y == null)
  16. {
  17. return false;
  18. }
  19. if (comparintFiledName.Length == 0)
  20. {
  21. return x.Equals(y);
  22. }
  23. bool result = true;
  24. var typeX = x.GetType();//获取类型
  25. var typeY = y.GetType();
  26. foreach (var filedName in comparintFiledName)
  27. {
  28. var xPropertyInfo = (from p in typeX.GetProperties() where p.Name.Equals(filedName) select p).FirstOrDefault();
  29. var yPropertyInfo = (from p in typeY.GetProperties() where p.Name.Equals(filedName) select p).FirstOrDefault();
  30. result = result
  31. && xPropertyInfo != null && yPropertyInfo != null
  32. && xPropertyInfo.GetValue(x, null).ToString().Equals(yPropertyInfo.GetValue(y, null));
  33. }
  34. return result;
  35. }
  36. int IEqualityComparer<T>.GetHashCode(T obj)
  37. {
  38. return obj.ToString().GetHashCode();
  39. }
  40. }
  41. }