I'm looking for a way to find the difference between the values of properties between two objects of the same type.
I've been working from the example in this stack overflow question:
Finding property differences between two C# objects
This is what I have so far:
public static string Compaire<T>(T initial, T final)
{
Type currentType = initial.GetType();
PropertyInfo[] props = currentType.GetProperties();
StringBuilder sb = new StringBuilder();
foreach (var prop in props)
{
Type equatable = prop.PropertyType.GetInterface("System.IEquatable");
if (equatable != null)
{
var i = prop.GetValue(initial);
var f = prop.GetValue(final);
if (i!= null && f != null && !i.Equals(f))
{
sb.Append(String.Format(_"{0}.{1} has changed from {2} to {3}. ", currentType.BaseType.Name, prop.Name, i, f));
}
}
}
return sb.ToString();
}
This is working for most cases however, nullable properties (like Nullable int
for example) are not getting compared because (I think) they are not being unboxed and nullable isn't implemented with IEquatable.
Using this method of reflection, is it possible to compare nullables while still avoiding entities that have been disposed (e.g. "The ObjectContext instance has been disposed and can no longer be used for operations that require a connection")?
Aucun commentaire:
Enregistrer un commentaire