Advertisement
Guest User

ReflectionComparerWithOptionalParams

a guest
Oct 18th, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.45 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Reflection;
  6.  
  7. public class Program
  8. {
  9.     public static void Main()
  10.     {
  11.         var time = DateTime.Now;
  12.         var a = new Parent()
  13.         {
  14.             Name = "A",
  15.             Age = 25,
  16.             FirstChild = new FirstChild
  17.             {
  18.                 Name = "B",
  19.                 Age = 12
  20.             },
  21.             Birthday = time,
  22.             TicketNumbers = new int[]
  23.             {
  24.                 1,2,3,4
  25.             },
  26.             ChildList = new List<FirstChild>()
  27.             {
  28.                 new FirstChild
  29.             {
  30.                 Name = "C",
  31.                 Age = 12
  32.             },new FirstChild
  33.             {
  34.                 Name = "D",
  35.                 Age = 12
  36.             },
  37.             }
  38.         };
  39.         var b = new Parent()
  40.         {
  41.             Name = "A",
  42.             Age = 25,
  43.             FirstChild = new FirstChild
  44.             {
  45.                 Name = "B",
  46.                 Age = 12
  47.             },
  48.             Birthday = time,
  49.             TicketNumbers = new int[]
  50.             {
  51.                  1,2,3,4
  52.             },
  53.             ChildList = new List<FirstChild>()
  54.             {
  55.                 new FirstChild
  56.             {
  57.                 Name = "C",
  58.                 Age = 12
  59.             },new FirstChild
  60.             {
  61.                 Name = "Dasd",
  62.                 Age = 12
  63.             },
  64.             }
  65.         };
  66.  
  67.         var result = CompareObjects(a, b, new[] { "Name" });
  68.         Console.WriteLine();
  69.     }
  70.  
  71.     public static bool CompareObjects(object inputObjectA, object inputObjectB, string[] ignorePropertiesList = null)
  72.     {
  73.         bool areObjectsEqual = true;
  74.         if (inputObjectA != null && inputObjectB != null)
  75.         {
  76.             object value1, value2;
  77.  
  78.             PropertyInfo[] properties = inputObjectA
  79.                 .GetType()
  80.                 .GetProperties(BindingFlags.Public | BindingFlags.Instance);
  81.  
  82.             foreach (PropertyInfo propertyInfo in properties)
  83.             {
  84.                 if (areObjectsEqual == false)
  85.                 {
  86.                     return false;
  87.                 }
  88.                 if (!propertyInfo.CanRead)
  89.                 {
  90.                     continue;
  91.                 }
  92.                 if (ignorePropertiesList != null && ignorePropertiesList.Length != 0 && ignorePropertiesList.Contains(propertyInfo.Name))
  93.                 {
  94.                     continue;
  95.                 }
  96.  
  97.                 value1 = propertyInfo.GetValue(inputObjectA, null);
  98.                 value2 = propertyInfo.GetValue(inputObjectB, null);
  99.  
  100.                 if (IsAssignableFrom(propertyInfo.PropertyType)
  101.                     || IsPrimitiveType(propertyInfo.PropertyType)
  102.                     || IsValueType(propertyInfo.PropertyType))
  103.                 {
  104.                     if (!AreValuesEqual(value1, value2))
  105.                     {
  106.                         Console.WriteLine("Property Name {0}", propertyInfo.Name);
  107.                         areObjectsEqual = false;
  108.                     }
  109.                 }
  110.                 else if (IsEnumerableType(propertyInfo.PropertyType))
  111.                 {
  112.                     Console.WriteLine("Property Name {0}", propertyInfo.Name);
  113.                     EnumComparer(value1, value2, ignorePropertiesList);
  114.                 }
  115.                 else if (propertyInfo.PropertyType.IsClass)
  116.                 {
  117.                     if (!CompareObjects(
  118.                         propertyInfo.GetValue(inputObjectA, null),
  119.                         propertyInfo.GetValue(inputObjectB, null),
  120.                         ignorePropertiesList))
  121.                     {
  122.                         areObjectsEqual = false;
  123.                     }
  124.                 }
  125.                 else
  126.                 {
  127.                     areObjectsEqual = false;
  128.                 }
  129.             }
  130.         }
  131.         else
  132.         {
  133.             areObjectsEqual = false;
  134.         }
  135.  
  136.         return areObjectsEqual;
  137.     }
  138.  
  139.     private static bool IsAssignableFrom(Type type)
  140.     {
  141.         return typeof(IComparable).IsAssignableFrom(type);
  142.     }
  143.  
  144.     private static bool IsPrimitiveType(Type type)
  145.     {
  146.         return type.IsPrimitive;
  147.     }
  148.  
  149.     private static bool IsValueType(Type type)
  150.     {
  151.         return type.IsValueType;
  152.     }
  153.  
  154.     private static bool IsEnumerableType(Type type)
  155.     {
  156.         return (typeof(IEnumerable).IsAssignableFrom(type));
  157.     }
  158.  
  159.     private static bool AreValuesEqual(object value1, object value2)
  160.     {
  161.         bool areValuesEqual = true;
  162.         var selfValueComparer = value1 as IComparable;
  163.  
  164.         if (value1 == null && value2 != null || value1 != null && value2 == null)
  165.         {
  166.             areValuesEqual = false;
  167.         }
  168.         else if (selfValueComparer != null && selfValueComparer.CompareTo(value2) != 0)
  169.         {
  170.             areValuesEqual = false;
  171.         }
  172.         else if (!object.Equals(value1, value2))
  173.         {
  174.             areValuesEqual = false;
  175.         }
  176.  
  177.         return areValuesEqual;
  178.     }
  179.  
  180.     private static bool EnumComparer(object first, object second, string[] ignorePropertiesList)
  181.     {
  182.         if (first == null && second != null || first != null && second == null)
  183.         {
  184.             return false;
  185.         }
  186.         else if (first != null && second != null)
  187.         {
  188.             IEnumerable<object> firstEnumeration;
  189.             IEnumerable<object> secondEnumeration;
  190.  
  191.             firstEnumeration = ((IEnumerable)first).Cast<object>();
  192.             secondEnumeration = ((IEnumerable)second).Cast<object>();
  193.  
  194.             if (firstEnumeration.Count() != secondEnumeration.Count())
  195.             {
  196.                 return false;
  197.             }
  198.             else
  199.             {
  200.                 object firstEnumarationItem;
  201.                 object secondEnumerationItem;
  202.  
  203.                 Type firstEnumarationItemType;
  204.                 for (int i = 0; i < firstEnumeration.Count(); i++)
  205.                 {
  206.                     firstEnumarationItem = firstEnumeration.ElementAt(i);
  207.                     secondEnumerationItem = secondEnumeration.ElementAt(i);
  208.  
  209.                     firstEnumarationItemType = firstEnumarationItem.GetType();
  210.                     if (IsAssignableFrom(firstEnumarationItemType)
  211.                         || IsPrimitiveType(firstEnumarationItemType)
  212.                         || IsValueType(firstEnumarationItemType))
  213.                     {
  214.                         if (!AreValuesEqual(firstEnumarationItem, secondEnumerationItem))
  215.                         {
  216.                             return false;
  217.                         }
  218.                     }
  219.                     else if (!CompareObjects(firstEnumarationItem, secondEnumerationItem, ignorePropertiesList))
  220.                     {
  221.                         return false;
  222.                     }
  223.                 }
  224.             }
  225.         }
  226.         return true;
  227.     }
  228. }
  229.  
  230. public class Parent
  231. {
  232.     public Parent()
  233.     {
  234.         this.ChildList = new List<FirstChild>();
  235.         this.FirstChild = new FirstChild();
  236.     }
  237.  
  238.     public string Name { get; set; }
  239.  
  240.     public int Age { get; set; }
  241.  
  242.     public FirstChild FirstChild { get; set; }
  243.  
  244.     public List<FirstChild> ChildList { get; set; }
  245.  
  246.     public int[] TicketNumbers { get; set; }
  247.  
  248.     public DateTime Birthday { get; set; }
  249. }
  250.  
  251. public class FirstChild
  252. {
  253.     public string Name { get; set; }
  254.  
  255.     public int Age { get; set; }
  256. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement