Skip to content

Instantly share code, notes, and snippets.

@sabbiryan
Created February 10, 2020 05:31
Show Gist options
  • Save sabbiryan/8d2ed32c177909bd679a476a055e75eb to your computer and use it in GitHub Desktop.
Save sabbiryan/8d2ed32c177909bd679a476a055e75eb to your computer and use it in GitHub Desktop.
Dynamic object property value compare. Compare two object of same model type with different value and get the changed and matched property collection in a response with status.
public static class ComparerExtensions
{
public static ObjectCompareResponse DeepCompare(this object source, object comparer)
{
var response = new ObjectCompareResponse();
if (ReferenceEquals(source, comparer)) return response;
if ((source == null) || (comparer == null))
{
response.MarkAsNotEqual();
return response;
}
//Compare two object's class, return false if they are difference
if (source.GetType() != comparer.GetType())
{
response.MarkAsNotEqual();
return response;
}
//Get all properties of source
//And compare each with comparer
foreach (var property in source.GetType().GetProperties())
{
var sourceValue = property.GetValue(source);
var comparerValue = property.GetValue(comparer);
if (sourceValue == null)
{
if (comparerValue == null)
{
response.AddToEqual(new ObjectCompareMatchProperty()
{
Name = property,
Value = null
});
}
else
{
response.AddToChange(new ObjectCompareChangeProperty()
{
Name = property,
OldValue = null,
NewValue = comparerValue
});
}
continue;
}
if (!sourceValue.Equals(comparerValue))
{
response.AddToChange(new ObjectCompareChangeProperty()
{
Name = property,
OldValue = sourceValue,
NewValue = comparerValue
});
}
else
{
response.AddToEqual(new ObjectCompareMatchProperty()
{
Name = property,
Value = sourceValue
});
}
}
return response;
}
}
public class ObjectCompareChangeProperty
{
public PropertyInfo Name { get; set; }
public object OldValue { get; set; }
public object NewValue { get; set; }
}
public class ObjectCompareMatchProperty
{
public PropertyInfo Name { get; set; }
public object Value { get; set; }
}
public class ObjectCompareResponse
{
private bool IsEquality { get; set; } = true;
public bool IsEqual => IsEquality && ChangeProperties.Count == 0;
public List<ObjectCompareMatchProperty> EqualProperties { get; set; } = new List<ObjectCompareMatchProperty>();
public List<ObjectCompareChangeProperty> ChangeProperties { get; set; } = new List<ObjectCompareChangeProperty>();
public void MarkAsEqual()
{
this.IsEquality = true;
}
public void MarkAsNotEqual()
{
this.IsEquality = false;
}
public void AddToEqual(ObjectCompareMatchProperty property)
{
this.EqualProperties.Add(property);
}
public void AddToChange(ObjectCompareChangeProperty property)
{
this.ChangeProperties.Add(property);
}
}
class Program
{
static void Main(string[] args)
{
//Create the two student object
var obj1 = new Student
{
FirstName = "Sabbir",
LastName = "Ahamed",
Email = "info@sabbirahamed.com"
}
var obj2 = new Student
{
FirstName = "Foyzul",
LastName = "Karim",
Email = "info@foyzulkarim.com"
}
//How to user it
var compareResponse = obj1.DeepCompare(obj2);
if (!compareResponse.IsEqual)
{
foreach (var changeProperty in compareResponse.ChangeProperties)
{
var oldVValue = changeProperty.OldValue?.ToString();
var newVValue = changeProperty.NewValue?.ToString();
Console.WriteLine($"Old value: {oldVValue} \t New value: {newVValue}");
}
}
}
}
public class Student
{
public string FirstName {get; set;}
public string LastName {get; set;}
public string Email {get; set;}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment