Gets a list of MemberComparison values that represent the fields and/or properties that differ between the two objects. Type of object to compare. First object to compare. Second object to compare. Returns list of structs with all different fields a
(this T first, T second)
| 20 | /// <param name="second">Second object to compare.</param> |
| 21 | /// <returns>Returns list of <see cref="MemberComparison" /> structs with all different fields and properties.</returns> |
| 22 | public static List<MemberComparison> ReflectiveCompare<T>(this T first, T second) |
| 23 | { |
| 24 | if (first.GetType() != second.GetType()) |
| 25 | throw new ArgumentException("both first and second parameters has to be of the same type"); |
| 26 | |
| 27 | var list = new List<MemberComparison>(); |
| 28 | var members = first.GetType().GetMembers(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public); |
| 29 | for (int i = 0; i < members.Length; i++) |
| 30 | { |
| 31 | var m = members[i]; |
| 32 | if (m.MemberType == MemberTypes.Field) |
| 33 | { |
| 34 | var f = (FieldInfo)m; |
| 35 | var xValue = f.GetValue(first); |
| 36 | var yValue = f.GetValue(second); |
| 37 | if (!Equals(xValue, yValue)) |
| 38 | { |
| 39 | list.Add(new MemberComparison(new ScriptMemberInfo(f), xValue, yValue)); |
| 40 | } |
| 41 | } |
| 42 | else if (m.MemberType == MemberTypes.Property) |
| 43 | { |
| 44 | var p = (PropertyInfo)m; |
| 45 | if (p.CanRead && p.GetGetMethod().GetParameters().Length == 0) |
| 46 | { |
| 47 | var xValue = p.GetValue(first, null); |
| 48 | var yValue = p.GetValue(second, null); |
| 49 | if (!Equals(xValue, yValue)) |
| 50 | { |
| 51 | list.Add(new MemberComparison(new ScriptMemberInfo(p), xValue, yValue)); |
| 52 | } |
| 53 | } |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | return list; |
| 58 | } |
| 59 | } |
| 60 | } |
nothing calls this directly
no test coverage detected