Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Linq;
- using System.Reflection;
- public class Program
- {
- public static void Main()
- {
- var time = DateTime.Now;
- var a = new Parent()
- {
- Name = "A",
- Age = 25,
- FirstChild = new FirstChild
- {
- Name = "B",
- Age = 12
- },
- Birthday = time,
- TicketNumbers = new int[]
- {
- 1,2,3,4
- },
- ChildList = new List<FirstChild>()
- {
- new FirstChild
- {
- Name = "C",
- Age = 12
- },new FirstChild
- {
- Name = "D",
- Age = 12
- },
- }
- };
- var b = new Parent()
- {
- Name = "A",
- Age = 25,
- FirstChild = new FirstChild
- {
- Name = "B",
- Age = 12
- },
- Birthday = time,
- TicketNumbers = new int[]
- {
- 1,2,3,4
- },
- ChildList = new List<FirstChild>()
- {
- new FirstChild
- {
- Name = "C",
- Age = 12
- },new FirstChild
- {
- Name = "Dasd",
- Age = 12
- },
- }
- };
- var result = CompareObjects(a, b, new[] { "Name" });
- Console.WriteLine();
- }
- public static bool CompareObjects(object inputObjectA, object inputObjectB, string[] ignorePropertiesList = null)
- {
- bool areObjectsEqual = true;
- if (inputObjectA != null && inputObjectB != null)
- {
- object value1, value2;
- PropertyInfo[] properties = inputObjectA
- .GetType()
- .GetProperties(BindingFlags.Public | BindingFlags.Instance);
- foreach (PropertyInfo propertyInfo in properties)
- {
- if (areObjectsEqual == false)
- {
- return false;
- }
- if (!propertyInfo.CanRead)
- {
- continue;
- }
- if (ignorePropertiesList != null && ignorePropertiesList.Length != 0 && ignorePropertiesList.Contains(propertyInfo.Name))
- {
- continue;
- }
- value1 = propertyInfo.GetValue(inputObjectA, null);
- value2 = propertyInfo.GetValue(inputObjectB, null);
- if (IsAssignableFrom(propertyInfo.PropertyType)
- || IsPrimitiveType(propertyInfo.PropertyType)
- || IsValueType(propertyInfo.PropertyType))
- {
- if (!AreValuesEqual(value1, value2))
- {
- Console.WriteLine("Property Name {0}", propertyInfo.Name);
- areObjectsEqual = false;
- }
- }
- else if (IsEnumerableType(propertyInfo.PropertyType))
- {
- Console.WriteLine("Property Name {0}", propertyInfo.Name);
- EnumComparer(value1, value2, ignorePropertiesList);
- }
- else if (propertyInfo.PropertyType.IsClass)
- {
- if (!CompareObjects(
- propertyInfo.GetValue(inputObjectA, null),
- propertyInfo.GetValue(inputObjectB, null),
- ignorePropertiesList))
- {
- areObjectsEqual = false;
- }
- }
- else
- {
- areObjectsEqual = false;
- }
- }
- }
- else
- {
- areObjectsEqual = false;
- }
- return areObjectsEqual;
- }
- private static bool IsAssignableFrom(Type type)
- {
- return typeof(IComparable).IsAssignableFrom(type);
- }
- private static bool IsPrimitiveType(Type type)
- {
- return type.IsPrimitive;
- }
- private static bool IsValueType(Type type)
- {
- return type.IsValueType;
- }
- private static bool IsEnumerableType(Type type)
- {
- return (typeof(IEnumerable).IsAssignableFrom(type));
- }
- private static bool AreValuesEqual(object value1, object value2)
- {
- bool areValuesEqual = true;
- var selfValueComparer = value1 as IComparable;
- if (value1 == null && value2 != null || value1 != null && value2 == null)
- {
- areValuesEqual = false;
- }
- else if (selfValueComparer != null && selfValueComparer.CompareTo(value2) != 0)
- {
- areValuesEqual = false;
- }
- else if (!object.Equals(value1, value2))
- {
- areValuesEqual = false;
- }
- return areValuesEqual;
- }
- private static bool EnumComparer(object first, object second, string[] ignorePropertiesList)
- {
- if (first == null && second != null || first != null && second == null)
- {
- return false;
- }
- else if (first != null && second != null)
- {
- IEnumerable<object> firstEnumeration;
- IEnumerable<object> secondEnumeration;
- firstEnumeration = ((IEnumerable)first).Cast<object>();
- secondEnumeration = ((IEnumerable)second).Cast<object>();
- if (firstEnumeration.Count() != secondEnumeration.Count())
- {
- return false;
- }
- else
- {
- object firstEnumarationItem;
- object secondEnumerationItem;
- Type firstEnumarationItemType;
- for (int i = 0; i < firstEnumeration.Count(); i++)
- {
- firstEnumarationItem = firstEnumeration.ElementAt(i);
- secondEnumerationItem = secondEnumeration.ElementAt(i);
- firstEnumarationItemType = firstEnumarationItem.GetType();
- if (IsAssignableFrom(firstEnumarationItemType)
- || IsPrimitiveType(firstEnumarationItemType)
- || IsValueType(firstEnumarationItemType))
- {
- if (!AreValuesEqual(firstEnumarationItem, secondEnumerationItem))
- {
- return false;
- }
- }
- else if (!CompareObjects(firstEnumarationItem, secondEnumerationItem, ignorePropertiesList))
- {
- return false;
- }
- }
- }
- }
- return true;
- }
- }
- public class Parent
- {
- public Parent()
- {
- this.ChildList = new List<FirstChild>();
- this.FirstChild = new FirstChild();
- }
- public string Name { get; set; }
- public int Age { get; set; }
- public FirstChild FirstChild { get; set; }
- public List<FirstChild> ChildList { get; set; }
- public int[] TicketNumbers { get; set; }
- public DateTime Birthday { get; set; }
- }
- public class FirstChild
- {
- public string Name { get; set; }
- public int Age { get; set; }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement