In that answer in the OS I found something that is more or less what you want.
You have to think if it pays to use this, you have to use it a lot.
It’s slower and less reliable. Maybe you need to adapt something, who knows how to reverse the situation of the list to ignore if most of the time the list to ignore is too big.
It has other implications, if changing the object structure can change the behavior of this method and give different results than expected.
public static bool PublicInstancePropertiesEqual<T>(this T self, T to, params string[] ignore) where T : class {
if (self != null && to != null) {
var type = typeof(T);
var ignoreList = new List<string>(ignore);
var unequalProperties =
from pi in type.GetProperties(BindingFlags.Public | BindingFlags.Instance)
where !ignoreList.Contains(pi.Name)
let selfValue = type.GetProperty(pi.Name).GetValue(self, null)
let toValue = type.GetProperty(pi.Name).GetValue(to, null)
where selfValue != toValue && (selfValue == null || !selfValue.Equals(toValue))
select selfValue;
return !unequalProperties.Any();
}
return self == to;
}
I put in the Github for future reference.