Guest User

Untitled

a guest
Jan 24th, 2019
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.27 KB | None | 0 0
  1. public class Address
  2. {
  3. public string AddressLine1 { get; set; }
  4. public string AddressLine2 { get; set; }
  5. public string City { get; set; }
  6. public string State { get; set; }
  7. public string Zip { get; set; }
  8. }
  9.  
  10. public class Employee
  11. {
  12. public string FirstName { get; set; }
  13. public string MiddleName { get; set; }
  14. public string LastName { get; set; }
  15. public Address EmployeeAddress { get; set; }
  16. }
  17.  
  18. var emp1Address = new Address();
  19. emp1Address.AddressLine1 = "Microsoft Corporation";
  20. emp1Address.AddressLine2 = "One Microsoft Way";
  21. emp1Address.City = "Redmond";
  22. emp1Address.State = "WA";
  23. emp1Address.Zip = "98052-6399";
  24.  
  25. var emp1 = new Employee();
  26. emp1.FirstName = "Bill";
  27. emp1.LastName = "Gates";
  28. emp1.EmployeeAddress = emp1Address;
  29.  
  30.  
  31. var emp2Address = new Address();
  32. emp2Address.AddressLine1 = "Gates Foundation";
  33. emp2Address.AddressLine2 = "One Microsoft Way";
  34. emp2Address.City = "Redmond";
  35. emp2Address.State = "WA";
  36. emp2Address.Zip = "98052-6399";
  37.  
  38. var emp2 = new Employee();
  39. emp2.FirstName = "Melinda";
  40. emp2.LastName = "Gates";
  41. emp2.EmployeeAddress = emp2Address;
  42.  
  43. public static List<PropertyInfo> GetDifferences(Employee test1, Employee test2)
  44. {
  45. List<PropertyInfo> differences = new List<PropertyInfo>();
  46. foreach (PropertyInfo property in test1.GetType().GetProperties())
  47. {
  48. object value1 = property.GetValue(test1, null);
  49. object value2 = property.GetValue(test2, null);
  50. if (!value1.Equals(value2))
  51. {
  52. differences.Add(property);
  53. }
  54. }
  55. return differences;
  56. }
  57.  
  58. using System;
  59. using System.Collections.Generic;
  60. using System.Linq;
  61. using System.Reflection;
  62.  
  63. protected List<KeyValuePair<Type, PropertyInfo>> RecrusiveReflectionCompare<T>(T first, T second)
  64. where T : class
  65. {
  66. var differences = new List<KeyValuePair<Type, PropertyInfo>>();
  67.  
  68. var parentType = first.GetType();
  69.  
  70. void CompareObject(object obj1, object obj2, PropertyInfo info)
  71. {
  72. if (!obj1.Equals(obj2))
  73. {
  74. differences.Add(new KeyValuePair<Type, PropertyInfo>(parentType, info));
  75. }
  76. }
  77.  
  78. foreach (PropertyInfo property in parentType.GetProperties())
  79. {
  80. object value1 = property.GetValue(first, null);
  81. object value2 = property.GetValue(second, null);
  82.  
  83. if (property.PropertyType == typeof(string))
  84. {
  85. if (string.IsNullOrEmpty(value1 as string) != string.IsNullOrEmpty(value2 as string))
  86. {
  87. CompareObject(value1, value2, property);
  88. }
  89. }
  90. else if (property.PropertyType.IsPrimitive)
  91. {
  92. CompareObject(value1, value2, property);
  93. }
  94. else
  95. {
  96. if (value1 == null && value2 == null)
  97. {
  98. continue;
  99. }
  100.  
  101. differences.Concat(RecrusiveReflectionCompare(value1, value2));
  102. }
  103. }
  104. return differences;
  105. }
  106.  
  107. public IEnumerable<PropertyInfo> GetNotEqualsProperties(Employee emp1, Employee emp2)
  108. {
  109. Type employeeType = typeof (Employee);
  110. var properies = employeeType.GetProperties();
  111. foreach (var property in properies)
  112. if(!property.GetValue(emp1, null).Equals(property.GetValue(emp2, null))) //TODO: check for null
  113. yield return property;
  114. }
  115.  
  116. public class Address
  117. {
  118. public string AddressLine1 { get; set; }
  119. public string AddressLine2 { get; set; }
  120. public string City { get; set; }
  121. public string State { get; set; }
  122. public string Zip { get; set; }
  123.  
  124. public override bool Equals(object obj)
  125. {
  126. if (obj as Address == null)
  127. return false;
  128. return ((Address) obj).AddressLine1.Equals(AddressLine1);
  129. }
  130. }
  131.  
  132. public static IEnumerable<string> DiffEmployees
  133. (Employee one, Employee two)
  134. {
  135. if(one.FirstName != two.FirstName)
  136. yield return "FirstName";
  137. if(one.LastName != two.LastName)
  138. yield return "LastName";
  139. if(one.Address.AddressLine1 != two.Address.AddressLine1)
  140. yield return "Address.AddressLine1";
  141.  
  142. // And so on.
  143. }
Add Comment
Please, Sign In to add comment