@@ -14,6 +14,9 @@ internal sealed class ExpressionComparer : IEqualityComparer<Expression>
14
14
{
15
15
public static readonly ExpressionComparer Default = new ExpressionComparer ( ) ;
16
16
17
+ [ ThreadStatic ]
18
+ private static int quoteDepth = 0 ;
19
+
17
20
private ExpressionComparer ( )
18
21
{
19
22
}
@@ -30,15 +33,16 @@ public bool Equals(Expression x, Expression y)
30
33
return false ;
31
34
}
32
35
33
- // Before actually comparing two nodes, make sure that captures variables have been
34
- // evaluated to their current values (as we don't want to compare their identities):
36
+ // Before actually comparing two nodes, make sure that captured variables have been
37
+ // evaluated to their current values (as we don't want to compare their identities).
38
+ // But do so only for the main expression; leave quoted (nested) expressions unchanged.
35
39
36
- if ( x is MemberExpression )
40
+ if ( x is MemberExpression && ExpressionComparer . quoteDepth == 0 )
37
41
{
38
42
x = x . Apply ( EvaluateCaptures . Rewriter ) ;
39
43
}
40
44
41
- if ( y is MemberExpression )
45
+ if ( y is MemberExpression && ExpressionComparer . quoteDepth == 0 )
42
46
{
43
47
y = y . Apply ( EvaluateCaptures . Rewriter ) ;
44
48
}
@@ -47,13 +51,23 @@ public bool Equals(Expression x, Expression y)
47
51
{
48
52
switch ( x . NodeType )
49
53
{
54
+ case ExpressionType . Quote :
55
+ ExpressionComparer . quoteDepth ++ ;
56
+ try
57
+ {
58
+ return this . EqualsUnary ( ( UnaryExpression ) x , ( UnaryExpression ) y ) ;
59
+ }
60
+ finally
61
+ {
62
+ ExpressionComparer . quoteDepth -- ;
63
+ }
64
+
50
65
case ExpressionType . Negate :
51
66
case ExpressionType . NegateChecked :
52
67
case ExpressionType . Not :
53
68
case ExpressionType . Convert :
54
69
case ExpressionType . ConvertChecked :
55
70
case ExpressionType . ArrayLength :
56
- case ExpressionType . Quote :
57
71
case ExpressionType . TypeAs :
58
72
case ExpressionType . UnaryPlus :
59
73
return this . EqualsUnary ( ( UnaryExpression ) x , ( UnaryExpression ) y ) ;
0 commit comments