Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Address
- {
- public string AddressLine1 { get; set; }
- public string AddressLine2 { get; set; }
- public string City { get; set; }
- public string State { get; set; }
- public string Zip { get; set; }
- }
- public class Employee
- {
- public string FirstName { get; set; }
- public string MiddleName { get; set; }
- public string LastName { get; set; }
- public Address EmployeeAddress { get; set; }
- }
- var emp1Address = new Address();
- emp1Address.AddressLine1 = "Microsoft Corporation";
- emp1Address.AddressLine2 = "One Microsoft Way";
- emp1Address.City = "Redmond";
- emp1Address.State = "WA";
- emp1Address.Zip = "98052-6399";
- var emp1 = new Employee();
- emp1.FirstName = "Bill";
- emp1.LastName = "Gates";
- emp1.EmployeeAddress = emp1Address;
- var emp2Address = new Address();
- emp2Address.AddressLine1 = "Gates Foundation";
- emp2Address.AddressLine2 = "One Microsoft Way";
- emp2Address.City = "Redmond";
- emp2Address.State = "WA";
- emp2Address.Zip = "98052-6399";
- var emp2 = new Employee();
- emp2.FirstName = "Melinda";
- emp2.LastName = "Gates";
- emp2.EmployeeAddress = emp2Address;
- public static List<PropertyInfo> GetDifferences(Employee test1, Employee test2)
- {
- List<PropertyInfo> differences = new List<PropertyInfo>();
- foreach (PropertyInfo property in test1.GetType().GetProperties())
- {
- object value1 = property.GetValue(test1, null);
- object value2 = property.GetValue(test2, null);
- if (!value1.Equals(value2))
- {
- differences.Add(property);
- }
- }
- return differences;
- }
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Reflection;
- protected List<KeyValuePair<Type, PropertyInfo>> RecrusiveReflectionCompare<T>(T first, T second)
- where T : class
- {
- var differences = new List<KeyValuePair<Type, PropertyInfo>>();
- var parentType = first.GetType();
- void CompareObject(object obj1, object obj2, PropertyInfo info)
- {
- if (!obj1.Equals(obj2))
- {
- differences.Add(new KeyValuePair<Type, PropertyInfo>(parentType, info));
- }
- }
- foreach (PropertyInfo property in parentType.GetProperties())
- {
- object value1 = property.GetValue(first, null);
- object value2 = property.GetValue(second, null);
- if (property.PropertyType == typeof(string))
- {
- if (string.IsNullOrEmpty(value1 as string) != string.IsNullOrEmpty(value2 as string))
- {
- CompareObject(value1, value2, property);
- }
- }
- else if (property.PropertyType.IsPrimitive)
- {
- CompareObject(value1, value2, property);
- }
- else
- {
- if (value1 == null && value2 == null)
- {
- continue;
- }
- differences.Concat(RecrusiveReflectionCompare(value1, value2));
- }
- }
- return differences;
- }
- public IEnumerable<PropertyInfo> GetNotEqualsProperties(Employee emp1, Employee emp2)
- {
- Type employeeType = typeof (Employee);
- var properies = employeeType.GetProperties();
- foreach (var property in properies)
- if(!property.GetValue(emp1, null).Equals(property.GetValue(emp2, null))) //TODO: check for null
- yield return property;
- }
- public class Address
- {
- public string AddressLine1 { get; set; }
- public string AddressLine2 { get; set; }
- public string City { get; set; }
- public string State { get; set; }
- public string Zip { get; set; }
- public override bool Equals(object obj)
- {
- if (obj as Address == null)
- return false;
- return ((Address) obj).AddressLine1.Equals(AddressLine1);
- }
- }
- public static IEnumerable<string> DiffEmployees
- (Employee one, Employee two)
- {
- if(one.FirstName != two.FirstName)
- yield return "FirstName";
- if(one.LastName != two.LastName)
- yield return "LastName";
- if(one.Address.AddressLine1 != two.Address.AddressLine1)
- yield return "Address.AddressLine1";
- // And so on.
- }
Add Comment
Please, Sign In to add comment