Objects(same type) Deep comparison using C#

There are many situation like we need to compare objects for example, int a =7 and int b=9 so it is easy to compare like, if ( a > b) or if (a< b) or if (a ==b). But if the situation comes like


Person firstPerson = new Person { Name = "Person A", Address = "Person A's Address", IsResidence=YesNo.Yes };
Person secondPerson = new Person { Name = "Person B", Address = "Person B's Address", IsResidence = YesNo.Yes };


and we need to compare those two objects named firstPerson and secondPerson whether they are same or not. This comparison means, each of the property of the object's value has to be compare against the other one. I wrote a small program to do basic Value types comparison of  two given objects.


I wrote a class named ObjectsCompare with following code,

namespace DeepComparer
{
    using System.Reflection;
 
    public class ObjectsCompare
    {
        public bool IsEqual(object firstObject, object secondObject)
        {
            bool result = default(bool);
            foreach (PropertyInfo firstObjectPropertyInfo in firstObject.GetType().GetProperties())
            {
                foreach (PropertyInfo secondObjectPropertyInfo in secondObject.GetType().GetProperties())
                {
                    if (firstObjectPropertyInfo.Name == secondObjectPropertyInfo.Name)
                    {
                        result = firstObjectPropertyInfo.GetValue(firstObject, null).ToString() == secondObjectPropertyInfo.GetValue(secondObject, null).ToString();
                        if (!result) break;
                    }
                }
                if (!result) break;
            }
            return result;
        }
    }
}


The Test harness of the above code is as below,

namespace TestHarness
{
    using System;
    using DeepComparer;
    class Program
    {
        static void Main(string[] args)
        {
            ObjectsCompare comparer = new ObjectsCompare();
            Person firstPerson = new Person { Name = "Person A", Address = "Person A's Address", IsResidence=YesNo.Yes };
            Person firstPersonClone = firstPerson;
            Person secondPerson = new Person { Name = "Person B", Address = "Person B's Address", IsResidence = YesNo.Yes };
            Person secondPersonClone = secondPerson;
 
            if (comparer.IsEqual(firstPerson, secondPerson))
                Console.WriteLine("{0}""same");
            else
                Console.WriteLine("{0}""not same");
 
            if (comparer.IsEqual(firstPerson, firstPersonClone))
                Console.WriteLine("{0}""same");
            else
                Console.WriteLine("{0}""not same");
        }
    }
 
    public enum YesNo 
    {
        Yes=0,
        No
    }
    public class Person
    {
        public string Name { getset; }
        public string Address { getset; }
        public YesNo IsResidence { getset; }
    }
}

The out put of the above code is as below,

not same
same
Press any key to continue . . .
Thanks mohammad.

No comments:

Post a Comment