-
-
Notifications
You must be signed in to change notification settings - Fork 193
/
Copy pathRootComparer.cs
142 lines (119 loc) · 4.56 KB
/
RootComparer.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
using System;
using System.Collections.Generic;
using System.Linq;
using KellermanSoftware.CompareNetObjects.TypeComparers;
using System.Reflection;
namespace KellermanSoftware.CompareNetObjects
{
/// <summary>
/// The base comparer which contains all the type comparers
/// </summary>
public class RootComparer : BaseComparer
{
#region Properties
/// <summary>
/// A list of the type comparers
/// </summary>
internal List<BaseTypeComparer> TypeComparers { get; set; }
#endregion
#region Methods
/// <summary>
/// Compare two objects
/// </summary>
public bool Compare(CompareParms parms)
{
try
{
if (parms.Object1 == null && parms.Object2 == null)
return true;
Type t1 = parms.Object1 != null ? parms.Object1.GetType() : null;
Type t2 = parms.Object2 != null ? parms.Object2.GetType() : null;
if (ExcludeLogic.ShouldExcludeType(parms.Config, t1, t2))
return true;
BaseTypeComparer customComparer = parms.Config.CustomComparers.FirstOrDefault(o => o.IsTypeMatch(t1, t2));
if (customComparer != null)
{
customComparer.CompareType(parms);
}
else if (parms.CustomPropertyComparer != null)
{
parms.CustomPropertyComparer.CompareType(parms);
}
else
{
if (!PerformBaseTypeComparison(parms, t1, t2)) return false;
}
}
catch (ObjectDisposedException)
{
if (!parms.Config.IgnoreObjectDisposedException)
throw;
return true;
}
return parms.Result.AreEqual;
}
private bool PerformBaseTypeComparison(CompareParms parms, Type t1, Type t2)
{
BaseTypeComparer typeComparer = TypeComparers.FirstOrDefault(o => o.IsTypeMatch(t1, t2));
if (typeComparer != null)
{
if (parms.Config.IgnoreObjectTypes || !TypesDifferent(parms, t1, t2))
{
typeComparer.CompareType(parms);
}
}
else
{
if (EitherObjectIsNull(parms)) return false;
if (!parms.Config.IgnoreObjectTypes && t1 != null)
throw new NotSupportedException("Cannot compare object of type " + t1.Name);
}
return true;
}
private bool TypesDifferent(CompareParms parms, Type t1, Type t2)
{
//Objects must be the same type and not be null
if (!parms.Config.IgnoreObjectTypes
&& parms.Object1 != null
&& parms.Object2 != null
&& t1 != t2)
{
//Only care if they are in the same inheritance hierarchy or decleared as the same interface.
if (parms.Config.IgnoreConcreteTypes
&& (parms.Object1DeclaredType != null
&& parms.Object2DeclaredType != null
&& parms.Object1DeclaredType == parms.Object2DeclaredType
|| (t1.IsAssignableFrom(t2) || t2.IsAssignableFrom(t1))))
{
return false;
}
Difference difference = new Difference
{
ParentObject1 = parms.ParentObject1,
ParentObject2 = parms.ParentObject2,
PropertyName = parms.BreadCrumb,
Object1Value = t1.FullName,
Object2Value = t2.FullName,
ChildPropertyName = "GetType()",
MessagePrefix = "Different Types",
Object1 = parms.Object1,
Object2 = parms.Object2
};
AddDifference(parms.Result, difference);
return true;
}
return false;
}
private bool EitherObjectIsNull(CompareParms parms)
{
//Check if one of them is null
if (parms.Object1 == null || parms.Object2 == null)
{
AddDifference(parms);
return true;
}
return false;
}
#endregion
}
}