Skip to content

Commit 5b5350f

Browse files
committed
ReferenceEquals
1 parent a656858 commit 5b5350f

3 files changed

Lines changed: 28 additions & 2 deletions

File tree

src/System.Linq.Dynamic.Core/Parser/PredefinedMethodsHelper.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,15 @@ internal static class PredefinedMethodsHelper
99
internal static readonly MethodInfo ObjectToString = typeof(object).GetMethod(nameof(ToString), BindingFlags.Instance | BindingFlags.Public, null, Type.EmptyTypes, null)!;
1010
internal static readonly MethodInfo ObjectInstanceEquals = typeof(object).GetMethod(nameof(Equals), BindingFlags.Instance | BindingFlags.Public, null, [typeof(object)], null)!;
1111
internal static readonly MethodInfo ObjectStaticEquals = typeof(object).GetMethod(nameof(Equals), BindingFlags.Static | BindingFlags.Public, null, [typeof(object), typeof(object)], null)!;
12+
internal static readonly MethodInfo ObjectStaticReferenceEquals = typeof(object).GetMethod(nameof(ReferenceEquals), BindingFlags.Static | BindingFlags.Public, null, [typeof(object), typeof(object)], null)!;
13+
1214

1315
private static readonly HashSet<MemberInfo> ObjectToStringAndObjectEquals =
1416
[
1517
ObjectToString,
1618
ObjectInstanceEquals,
17-
ObjectStaticEquals
19+
ObjectStaticEquals,
20+
ObjectStaticReferenceEquals
1821
];
1922

2023
public static bool IsPredefinedMethod(ParsingConfig config, MemberInfo member)

src/System.Linq.Dynamic.Core/ParsingConfig.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ public IQueryableAnalyzer QueryableAnalyzer
314314
public bool RestrictOrderByToPropertyOrField { get; set; }
315315

316316
/// <summary>
317-
/// When set to <c>true</c>, the parser will allow the use of the Equals(object obj), Equals(object objA, object objB) and ToString() methods on the <see cref="object"/> type.
317+
/// When set to <c>true</c>, the parser will allow the use of the Equals(object obj), Equals(object objA, object objB), ReferenceEquals(object objA, object objB) and ToString() methods on the <see cref="object"/> type.
318318
///
319319
/// Default value is <c>false</c>.
320320
/// </summary>

test/System.Linq.Dynamic.Core.Tests/Parser/MethodFinderTest.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,27 @@ public void Method_Equals2_OnDynamicLinq_And_SystemLinq_ShouldBeEqual()
7373
// Assert
7474
Assert.Equal(((MethodCallExpression)expr.Body).Method.DeclaringType, ((MethodCallExpression)expression).Method.DeclaringType);
7575
}
76+
77+
[Fact]
78+
public void Method_ReferenceEquals_OnDynamicLinq_And_SystemLinq_ShouldBeEqual()
79+
{
80+
// Arrange
81+
var config = new ParsingConfig
82+
{
83+
AllowEqualsAndToStringMethodsOnObject = true
84+
};
85+
86+
// ReSharper disable once RedundantNameQualifier
87+
Expression<Func<int?, bool>> expr = x => object.ReferenceEquals("a", "b");
88+
89+
var selector = "object.ReferenceEquals(\"a\", \"b\")";
90+
var prm = Parameter(typeof(int?));
91+
var parser = new ExpressionParser([prm], selector, [], config);
92+
93+
// Act
94+
var expression = parser.Parse(null);
95+
96+
// Assert
97+
Assert.Equal(((MethodCallExpression)expr.Body).Method.DeclaringType, ((MethodCallExpression)expression).Method.DeclaringType);
98+
}
7699
}

0 commit comments

Comments
 (0)