andrew4582

Properties Equal

Sep 26th, 2013
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.90 KB | None | 0 0
  1.         public static bool PropertiesEqual<T>(T self, T to, Dictionary<string, Tuple<object, object>> differences, params string[] ignore) where T : class
  2.         {
  3.             if (self != null && to != null)
  4.             {
  5.                 Type type = typeof(T);
  6.                 List<string> ignoreList = new List<string>(ignore);
  7.                 foreach (System.Reflection.PropertyInfo pi in type.GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance))
  8.                 {
  9.                     if (!ignoreList.Contains(pi.Name))
  10.                     {
  11.                         object selfValue = type.GetProperty(pi.Name).GetValue(self, null);
  12.                         object toValue = type.GetProperty(pi.Name).GetValue(to, null);
  13.  
  14.                         if (selfValue != toValue && (selfValue == null || !selfValue.Equals(toValue)))
  15.                         {
  16.                             differences[pi.Name] = new Tuple<object, object>(selfValue, toValue);
  17.                             continue;
  18.                         }
  19.                     }
  20.                 }
  21.                 return differences.Count == 0;
  22.             }
  23.             return self == to;
  24.         }
Advertisement
Add Comment
Please, Sign In to add comment