Skip to content

Instantly share code, notes, and snippets.

@danielkillyevo
Last active January 30, 2022 15:57
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save danielkillyevo/5443439 to your computer and use it in GitHub Desktop.
Save danielkillyevo/5443439 to your computer and use it in GitHub Desktop.
c# object deep comparison
public class ObjectComparer
{
public static bool Equals(object left, object right)
{
//Compare the references
if (object.ReferenceEquals(right, null))
return false;
if (object.ReferenceEquals(left, right))
return true;
//Compare the types
if (left.GetType() != right.GetType())
return false;
//Get all property infos of the right object
var propertyInfos = right.GetType().GetProperties();
//Compare the property values of the left and right object
foreach (var propertyInfo in propertyInfos)
{
var othersValue = propertyInfo.GetValue(right);
var currentsValue = propertyInfo.GetValue(left);
if (othersValue == null && currentsValue == null)
continue;
//Comparison if the property is a generic (IList type)
if ((currentsValue is IList && propertyInfo.PropertyType.IsGenericType) ||
(othersValue is IList && propertyInfo.PropertyType.IsGenericType))
{
//here we work with dynamics because don't need to care about the generic type
dynamic cur = currentsValue;
dynamic oth = othersValue;
if (cur != null && cur.Count > 0)
{
var result = false;
foreach (object cVal in cur)
{
foreach (object oVal in oth)
{
//Recursively call the Equal method
var areEqual = Equals(cVal, oVal);
if (!areEqual) continue;
result = true;
break;
}
}
if (result == false)
return false;
}
}
else
{
//Comparison for properties of a non collection type
var curType = currentsValue.GetType();
//Comparison for primitive types
if (curType.IsValueType || currentsValue is string)
{
var areEquals = currentsValue.Equals(othersValue);
if (areEquals == false)
return false; //This is the out point for this methods
}
else
{
//values are complex/classes
//that's why we have to recursively call the Equals methods
var areEqual = Equals(currentsValue, othersValue);
if (areEqual == false)
return false;
}
}
}
return true;
}
}
@Baccanno
Copy link

aren't you missing something like the follwing on L.25 ?

if (othersValue == null ^ currentsValue == null)return false;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment