Comparing objects is really a big story because we don’t have any direct method to compare the object (Except reference comparison). If we want to compare the object reference then we can use Object .Equals () but it is not useful when we want to compare the object properties.
Equals () method compares the references not the properties or values of the object. So it will not any help when we have requirement to compare the object properties or values.
Let me take the small example for this. Check the below code-
private void btnEquals_Click(object sender, EventArgs e)
{
try
{
ClsEmployee objEmp1 = new ClsEmployee();
ClsEmployee objEmp2 = new ClsEmployee();
ClsEmployee objEmp3 = new ClsEmployee();
//Assigning value to objEmp1
objEmp1.intEmpId = 1;
objEmp1.intEmpName = "Jitendra";
//Assigning value to objEmp2
objEmp2.intEmpId = 1;
objEmp2.intEmpName = "Jitendra";
//Assigning objEmp3 with objEmp1 (Reference)
objEmp3 = objEmp1;
//Comparing objEmp1 and objEmp2
if (objEmp1.Equals(objEmp2))
{
MessageBox.Show("Object objEmp1 and objEmp2 are same");
}
else
{
MessageBox.Show("Object objEmp1 and objEmp2 are not same");
}
//Comparing objEmp1 and objEmp3
if (objEmp1.Equals(objEmp3))
{
MessageBox.Show("Object objEmp1 and objEmp3 are same");
}
else
{
MessageBox.Show("Object objEmp1 and objEmp3 not are not same");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
In above code, I have assigned the same value to objEmp1 and objEmp2 but
objEmp1.Equals(objEmp2) will return False because objEmp1 and objEmp2 represents different references.
objEmp1.Equals(objEmp3) will return True because before comparing the object I have assigned objEmp3 = objEmp1, so here both will represent the same reference.
So here is the question- What if we want to compare the object properties?
Solution-
Here we have solution for this- We can make use of Reflection. Using reflection we can compare thee the properties of object with the help of PropertyInfo class. This class provides the information about the property.
You can find the difference based on following points-
1. If the type of objects are not same
2. If number of properties in objects are not same
3. If any property is missing in second object
4. If value of properties are not same
5. If any object is null
For this add the below namespace-
using System.Reflection;
Check the below example-
//Employee Class
public class ClsEmployee
{
public Int32 intEmpId { get; set; }
public string intEmpName { get; set; }
public ClsDept objClsDept { get; set; }
}
//Dept Class
public class ClsDept
{
public Int32 intDeptId { get; set; }
public string intDeptName { get; set; }
}
//Code to compare object using PropertyInfo Class
private void btnCompareProInfo_Click(object sender, EventArgs e)
{
try
{
ClsEmployee objEmp1 = new ClsEmployee();
ClsEmployee objEmp2 = new ClsEmployee();
ClsEmployee objEmp3 = new ClsEmployee();
//Assigning value to objEmp1
objEmp1.intEmpId = 1;
objEmp1.intEmpName = "Jitendra";
objEmp1.objClsDept = new ClsDept();
objEmp1.objClsDept.intDeptId = 1;
objEmp1.objClsDept.intDeptName = "AC";
//Assigning value to objEmp2
objEmp2.intEmpId = 1;
objEmp2.intEmpName = "Jitendra";
objEmp2.objClsDept = new ClsDept();
objEmp2.objClsDept.intDeptId = 1;
objEmp2.objClsDept.intDeptName = "AC";
//Assigning value to objEmp3
objEmp3.intEmpId = 1;
objEmp3.intEmpName = "Jitendra";
//Comparing objEmp1 and objEmp2
if (CompareObjects(objEmp1, objEmp2))
{
MessageBox.Show("Object objEmp1 and objEmp2 are same");
}
else
{
MessageBox.Show("Object objEmp1 and objEmp2 are not same");
}
//Comparing objEmp1 and objEmp3
if (CompareObjects(objEmp1, objEmp3))
{
MessageBox.Show("Object objEmp1 and objEmp3 are same");
}
else
{
MessageBox.Show("Object objEmp1 and objEmp3 are not same");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
//Method to compare Object
private Boolean CompareObjects(object obj1, object obj2)
{
Boolean isEqual = true;
try
{
//If any object is null then returning false
if (obj1 == null || obj2 == null)
{
return false;
}
//If object types are different then returning false
if (obj1.GetType() != obj2.GetType())
{
return false;
}
//Getting list of properties
PropertyInfo[] obj1PInfo = obj1.GetType().GetProperties();
PropertyInfo[] obj2PInfo = obj2.GetType().GetProperties();
//If number of properties are not same then return false
if (obj1PInfo.Count() != obj2PInfo.Count())
{
return false;
}
//Looping each child property
for (int index = 0; index < obj1PInfo.Count(); index++)
{
//Getting child property
PropertyInfo objChild1PInfo = obj1PInfo[index];
Object propValue1 = objChild1PInfo.GetValue(obj1, null);
Object propValue2 = null;
//Checking same property is not present in second object then return false
if (obj2PInfo.Contains(objChild1PInfo) == false)
{
return false;
}
else
{
PropertyInfo objChild2PInfo = obj2PInfo[index];
propValue2 = objChild2PInfo.GetValue(obj2, null);
}
//If object contains any child property (as class type) then compare child properties also
if (Enum.IsDefined(typeof(System.TypeCode), objChild1PInfo.PropertyType.Name) == false)
{
isEqual = CompareObjects(propValue1, propValue2);
}
else
{
if (Convert.ToString(propValue1) != Convert.ToString(propValue2))
{
return false;
}
}
}
return isEqual;
}
catch (Exception ex)
{
throw ex;
}
}
}
In above code, I have written one method (CompareObjects()) to compare the object s.
Boolean CompareObjects(object obj1, object obj2)- In this method I have used PropertyInfo class get the information about each property in objects. This method will compare the object based on points as I have given above.
So the result of (CompareObjects(objEmp1, objEmp2)) will be True because both object have same values And result of (CompareObjects(objEmp1, objEmp3)) will be False because both object have different values.
Note- This code will also work if you have any child classes.
Equals () method compares the references not the properties or values of the object. So it will not any help when we have requirement to compare the object properties or values.
Let me take the small example for this. Check the below code-
private void btnEquals_Click(object sender, EventArgs e)
{
try
{
ClsEmployee objEmp1 = new ClsEmployee();
ClsEmployee objEmp2 = new ClsEmployee();
ClsEmployee objEmp3 = new ClsEmployee();
//Assigning value to objEmp1
objEmp1.intEmpId = 1;
objEmp1.intEmpName = "Jitendra";
//Assigning value to objEmp2
objEmp2.intEmpId = 1;
objEmp2.intEmpName = "Jitendra";
//Assigning objEmp3 with objEmp1 (Reference)
objEmp3 = objEmp1;
//Comparing objEmp1 and objEmp2
if (objEmp1.Equals(objEmp2))
{
MessageBox.Show("Object objEmp1 and objEmp2 are same");
}
else
{
MessageBox.Show("Object objEmp1 and objEmp2 are not same");
}
//Comparing objEmp1 and objEmp3
if (objEmp1.Equals(objEmp3))
{
MessageBox.Show("Object objEmp1 and objEmp3 are same");
}
else
{
MessageBox.Show("Object objEmp1 and objEmp3 not are not same");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
In above code, I have assigned the same value to objEmp1 and objEmp2 but
objEmp1.Equals(objEmp2) will return False because objEmp1 and objEmp2 represents different references.
objEmp1.Equals(objEmp3) will return True because before comparing the object I have assigned objEmp3 = objEmp1, so here both will represent the same reference.
So here is the question- What if we want to compare the object properties?
Solution-
Here we have solution for this- We can make use of Reflection. Using reflection we can compare thee the properties of object with the help of PropertyInfo class. This class provides the information about the property.
You can find the difference based on following points-
1. If the type of objects are not same
2. If number of properties in objects are not same
3. If any property is missing in second object
4. If value of properties are not same
5. If any object is null
For this add the below namespace-
using System.Reflection;
Check the below example-
//Employee Class
public class ClsEmployee
{
public Int32 intEmpId { get; set; }
public string intEmpName { get; set; }
public ClsDept objClsDept { get; set; }
}
//Dept Class
public class ClsDept
{
public Int32 intDeptId { get; set; }
public string intDeptName { get; set; }
}
//Code to compare object using PropertyInfo Class
private void btnCompareProInfo_Click(object sender, EventArgs e)
{
try
{
ClsEmployee objEmp1 = new ClsEmployee();
ClsEmployee objEmp2 = new ClsEmployee();
ClsEmployee objEmp3 = new ClsEmployee();
//Assigning value to objEmp1
objEmp1.intEmpId = 1;
objEmp1.intEmpName = "Jitendra";
objEmp1.objClsDept = new ClsDept();
objEmp1.objClsDept.intDeptId = 1;
objEmp1.objClsDept.intDeptName = "AC";
//Assigning value to objEmp2
objEmp2.intEmpId = 1;
objEmp2.intEmpName = "Jitendra";
objEmp2.objClsDept = new ClsDept();
objEmp2.objClsDept.intDeptId = 1;
objEmp2.objClsDept.intDeptName = "AC";
//Assigning value to objEmp3
objEmp3.intEmpId = 1;
objEmp3.intEmpName = "Jitendra";
//Comparing objEmp1 and objEmp2
if (CompareObjects(objEmp1, objEmp2))
{
MessageBox.Show("Object objEmp1 and objEmp2 are same");
}
else
{
MessageBox.Show("Object objEmp1 and objEmp2 are not same");
}
//Comparing objEmp1 and objEmp3
if (CompareObjects(objEmp1, objEmp3))
{
MessageBox.Show("Object objEmp1 and objEmp3 are same");
}
else
{
MessageBox.Show("Object objEmp1 and objEmp3 are not same");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
//Method to compare Object
private Boolean CompareObjects(object obj1, object obj2)
{
Boolean isEqual = true;
try
{
//If any object is null then returning false
if (obj1 == null || obj2 == null)
{
return false;
}
//If object types are different then returning false
if (obj1.GetType() != obj2.GetType())
{
return false;
}
//Getting list of properties
PropertyInfo[] obj1PInfo = obj1.GetType().GetProperties();
PropertyInfo[] obj2PInfo = obj2.GetType().GetProperties();
//If number of properties are not same then return false
if (obj1PInfo.Count() != obj2PInfo.Count())
{
return false;
}
//Looping each child property
for (int index = 0; index < obj1PInfo.Count(); index++)
{
//Getting child property
PropertyInfo objChild1PInfo = obj1PInfo[index];
Object propValue1 = objChild1PInfo.GetValue(obj1, null);
Object propValue2 = null;
//Checking same property is not present in second object then return false
if (obj2PInfo.Contains(objChild1PInfo) == false)
{
return false;
}
else
{
PropertyInfo objChild2PInfo = obj2PInfo[index];
propValue2 = objChild2PInfo.GetValue(obj2, null);
}
//If object contains any child property (as class type) then compare child properties also
if (Enum.IsDefined(typeof(System.TypeCode), objChild1PInfo.PropertyType.Name) == false)
{
isEqual = CompareObjects(propValue1, propValue2);
}
else
{
if (Convert.ToString(propValue1) != Convert.ToString(propValue2))
{
return false;
}
}
}
return isEqual;
}
catch (Exception ex)
{
throw ex;
}
}
}
In above code, I have written one method (CompareObjects()) to compare the object s.
Boolean CompareObjects(object obj1, object obj2)- In this method I have used PropertyInfo class get the information about each property in objects. This method will compare the object based on points as I have given above.
So the result of (CompareObjects(objEmp1, objEmp2)) will be True because both object have same values And result of (CompareObjects(objEmp1, objEmp3)) will be False because both object have different values.
Note- This code will also work if you have any child classes.
No comments:
Post a Comment