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 (type.IsSealed == true && type.IsGenericType == false)
{
if (!initialObj.Equals(currentObj))
{
returnMessage = "initialObj value :{" + initialObj.ToString() + "},
currentObj value :{" + currentObj.ToString() + "}";
}
}
else
{
for (int count = 0; count < propListInfo.Length; count++)
{
if (propListInfo[count].GetGetMethod().GetParameters().Length > 0)
{
if (count > 1 && type.Name.IndexOf("Dictionary") == -1)
{
int indexersTotal = 0;
for (int tmpCount = 0; tmpCount < propListInfo.Length; tmpCount++)
{
if (propListInfo[tmpCount].Name == "Count")
{
indexersTotal = (int)propListInfo[tmpCount].GetValue(
initialObj, null);
break;
}
}
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)
{
string str = ex.Message;
}
}
}
else
{
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
Growing up with the world wide web, witnessing many bubbles and the rise of information technology to becoming an indispensable part of human existence, I was intrigued and astonished by the drastic changes it brought to the way we lived then. I was drawn into this whirlpool of internet and technology as an enthusiast only to come out as a qualified software professional. Worked extensively on technologies, starting with Visual Basic, ASP, Oracle and MS SQL to WCF, .NET Core , Azure service fabric and Kubernetes. Architected applications for on-premises to hybrid clouds to cloud native environments.
Fortunately, I like what I do and work has been fulfilling.
It has been 20+ years of working for various organisations in several roles both technical and managerial but not a
dull day.