Click here to Skip to main content
12,243,534 members (45,743 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

16.7K views
11 bookmarked
Posted

Compare Objects for Any Change in Value

, 11 Nov 2008 CPOL
Rate this:
Please Sign up or sign in to vote.
Compare object members to the deepest thread for any value change

Introduction

Object comparison has always been a topic of debate. There are lot of ways to implement it, using comparer interface and implementing it, etc. But there is no generic way by which two similar objects of any type/class can be compared for the change in value of any of the members.

Using the Code

Just implement the below mentioned methods in a class of your own:

private static string CompareObjects(object initialObj, object currentObj)
{
    string returnMessage = string.Empty;

    Type type = initialObj.GetType();
    Type type2 = currentObj.GetType();
    PropertyInfo[] propListInfo = type.GetProperties();
    PropertyInfo[] propListInfo1 = type2.GetProperties();

    //if class type is native i.e. string, int, boolean, etc.
    if (type.IsSealed == true && type.IsGenericType == false)
    {
        if (!initialObj.Equals(currentObj))
        {
            returnMessage = "initialObj value :{" + initialObj.ToString() + "},
                currentObj value :{" + currentObj.ToString() + "}";
        }
    }
    else //class type is object
    {
        //loop through each property of object
        for (int count = 0; count < propListInfo.Length; count++)
        {
            //check if it is an indexed object -- special case
            if (propListInfo[count].GetGetMethod().GetParameters().Length > 0)
            {
                // check if  not dict object --dict object needs to be addressed
                if (count > 1 && type.Name.IndexOf("Dictionary") == -1)
                {
                    int indexersTotal = 0;
                    //get index of count
                    for (int tmpCount = 0; tmpCount < propListInfo.Length; tmpCount++)
                    {
                        if (propListInfo[tmpCount].Name == "Count")
                        {
                            indexersTotal = (int)propListInfo[tmpCount].GetValue(
                                initialObj, null);

                            break;
                        }
                    }
                    //indexersTotal = 
                    //   (int)propListInfo[count - 1].GetValue(initialObj, null);
                    try
                    {
                        for (int curIndex = 0; curIndex < indexersTotal; curIndex++)
                        {
                            object Obj1 = propListInfo[count].GetValue(initialObj,
                                new object[] { curIndex });
                            object Obj2 = propListInfo1[count].GetValue(currentObj,
                                new object[] { curIndex });
                            returnMessage = Compare(Obj1, Obj2);
                            if (returnMessage.Length > 0)
                            {
                                return returnMessage + " " + 
				propListInfo[count].ToString();
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        //consume this error as it is occurred because of dictionary
                        //setting collection
                        string str = ex.Message;
                    }
                }
            }
            else //not an indexed object
            {
                object Obj1 = propListInfo[count].GetValue(initialObj, null);
                object Obj2 = propListInfo1[count].GetValue(currentObj, null);
                returnMessage = Compare(Obj1, Obj2);
                if (returnMessage.Length > 0)
                {
                    return returnMessage + " " + propListInfo[count].ToString();
                }
            }
        }
    }

    public static string Compare(object initialObj, object currentObj)
    {
        string returnMessage = "";
        if (initialObj == null && currentObj != null)
        {
            returnMessage = "initialObj is Null Object";
        }
        else if (initialObj != null && currentObj == null)
        {
            returnMessage = "currentObj is Null Object";
        }
        else if (initialObj != null && currentObj != null)
        {
            returnMessage = CompareObjects(initialObj, currentObj);
        }
        return returnMessage;
    }

History

  • 11th November, 2008: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Vikramaditya S Shekhawat
Architect
India India
Software and technology enthusiast with 15+ years experience in IT industry. Envisage "technical people management" for achieving best results and most satisfied team members.

You may also be interested in...

Comments and Discussions

 
QuestionHow to compare html files and show diffrences? Pin
nidjain23-Aug-09 16:27
membernidjain23-Aug-09 16:27 
GeneralMy vote of 1 Pin
Jeffrey Schaefer16-Jun-09 9:06
memberJeffrey Schaefer16-Jun-09 9:06 
GeneralCompare .NET Objects Pin
Greg Finzer1-Jun-09 7:30
memberGreg Finzer1-Jun-09 7:30 
QuestionTest code properly before submitting. Pin
MS_Jocker11-Mar-09 17:35
memberMS_Jocker11-Mar-09 17:35 
GeneralFaulty logic Pin
MKauffman11-Nov-08 7:34
memberMKauffman11-Nov-08 7:34 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.160426.1 | Last Updated 11 Nov 2008
Article Copyright 2008 by Vikramaditya S Shekhawat
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid