Compare.cs 999 B

123456789101112131415161718192021222324252627282930
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Reflection;
  5. namespace HyperCube
  6. {
  7. public static class Compare
  8. {
  9. public static Dictionary<string, PropertyInfo> SimpleCompare<T>(T object1, T object2)
  10. {
  11. var props = typeof(T).GetProperties().Where(pi => pi.CanRead);
  12. Dictionary<string, PropertyInfo> result = new();
  13. foreach (var prop in props)
  14. {
  15. var val1 = prop.GetValue(object1, null);
  16. var val2 = prop.GetValue(object2, null);
  17. if (prop.PropertyType == typeof(DateTime))
  18. {
  19. TimeSpan timeSpan = (DateTime)val1 - (DateTime)val2;
  20. if (timeSpan.TotalDays != 0)
  21. result[prop.Name] = prop;
  22. }
  23. else if (val1?.ToString() != val2?.ToString())
  24. result[prop.Name] = prop;
  25. }
  26. return result;
  27. }
  28. }
  29. }