-
-
Save huseyint/1026602 to your computer and use it in GitHub Desktop.
Property equality comparison on two objects
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Shamelessly stolen and adapted from http://stackoverflow.com/questions/506096/comparing-object-properties-in-c | |
public static class Comparisons | |
{ | |
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); | |
foreach (var propertyName in type.GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance).Select(pi => pi.Name).Except(ignore)) | |
{ | |
var selfValue = type.GetProperty(propertyName).GetValue(self, null); | |
var toValue = type.GetProperty(propertyName).GetValue(to, null); | |
if (selfValue != toValue && (selfValue == null || !selfValue.Equals(toValue))) | |
return false; | |
} | |
return true; | |
} | |
return self == to; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment