Skip to content

Commit 3101ebd

Browse files
committed
...
1 parent c25c3a3 commit 3101ebd

File tree

3 files changed

+43
-33
lines changed

3 files changed

+43
-33
lines changed

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public class ExpressionParser
3939
private readonly ConstantExpressionHelper _constantExpressionHelper;
4040
private readonly ITypeFinder _typeFinder;
4141
private readonly ITypeConverterFactory _typeConverterFactory;
42+
private readonly PredefinedMethodsHelper _predefinedMethodsHelper;
4243
private readonly Dictionary<string, object> _internals = new();
4344
private readonly Dictionary<string, object?> _symbols;
4445
private readonly bool _usedForOrderBy;
@@ -99,6 +100,7 @@ public ExpressionParser(ParameterExpression[]? parameters, string expression, ob
99100
_typeFinder = new TypeFinder(_parsingConfig, _keywordsHelper);
100101
_typeConverterFactory = new TypeConverterFactory(_parsingConfig);
101102
_constantExpressionHelper = ConstantExpressionHelperFactory.GetInstance(_parsingConfig);
103+
_predefinedMethodsHelper = new PredefinedMethodsHelper(_parsingConfig);
102104

103105
if (parameters != null)
104106
{
@@ -1854,7 +1856,12 @@ private Expression ParseMemberAccess(Type? type, Expression? expression, string?
18541856

18551857
case 1:
18561858
var method = (MethodInfo)methodBase!;
1857-
if (!PredefinedTypesHelper.IsPredefinedType(_parsingConfig, method.DeclaringType!) && !PredefinedMethodsHelper.IsPredefinedMethod(_parsingConfig, method))
1859+
1860+
int y = 0;
1861+
y.ToString();
1862+
1863+
if (!PredefinedTypesHelper.IsPredefinedType(_parsingConfig, method.DeclaringType!) &&
1864+
!_predefinedMethodsHelper.IsPredefinedMethod(type!, method))
18581865
{
18591866
throw ParseError(errorPos, Res.MethodIsInaccessible, id, TypeHelper.GetTypeName(method.DeclaringType!));
18601867
}

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

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Collections.Generic;
2+
using System.Linq.Dynamic.Core.Validation;
23
using System.Reflection;
34

45
namespace System.Linq.Dynamic.Core.Parser;
@@ -12,45 +13,47 @@ internal class PredefinedMethodsHelper
1213
internal static readonly MethodInfo ObjectStaticEquals = typeof(object).GetMethod(nameof(Equals), BindingFlags.Static | BindingFlags.Public, null, [typeof(object), typeof(object)], null)!;
1314
internal static readonly MethodInfo ObjectStaticReferenceEquals = typeof(object).GetMethod(nameof(ReferenceEquals), BindingFlags.Static | BindingFlags.Public, null, [typeof(object), typeof(object)], null)!;
1415

15-
private readonly Dictionary<Type, HashSet<MethodInfo>> _supported = new()
16+
private readonly Dictionary<Type, HashSet<MemberInfo>> _supported = new()
1617
{
17-
{ typeof(bool), new HashSet<MethodInfo>() },
18-
{ typeof(char), new HashSet<MethodInfo>() },
19-
{ typeof(string), new HashSet<MethodInfo>() },
20-
{ typeof(sbyte), new HashSet<MethodInfo>() },
21-
{ typeof(byte), new HashSet<MethodInfo>() },
22-
{ typeof(short), new HashSet<MethodInfo>() },
23-
{ typeof(ushort), new HashSet<MethodInfo>() },
24-
{ typeof(int), new HashSet<MethodInfo>() },
25-
{ typeof(uint), new HashSet<MethodInfo>() },
26-
{ typeof(long), new HashSet<MethodInfo>() },
27-
{ typeof(ulong), new HashSet<MethodInfo>() },
28-
{ typeof(float), new HashSet<MethodInfo>() },
29-
{ typeof(double), new HashSet<MethodInfo>() },
30-
{ typeof(decimal), new HashSet<MethodInfo>() },
31-
// { typeof(DateTime), new HashSet<MethodInfo>() },
32-
// { typeof(DateTimeOffset), new HashSet<MethodInfo>() },
33-
// { typeof(TimeSpan), new HashSet<MethodInfo>() },
34-
// { typeof(Guid), new HashSet<MethodInfo>() },
35-
// { typeof(Uri), new HashSet<MethodInfo>() },
36-
// { typeof(Enum), new HashSet<MethodInfo>() },
18+
{ typeof(bool), new HashSet<MemberInfo>() },
19+
{ typeof(char), new HashSet<MemberInfo>() },
20+
{ typeof(string), new HashSet<MemberInfo>() },
21+
{ typeof(sbyte), new HashSet<MemberInfo>() },
22+
{ typeof(byte), new HashSet<MemberInfo>() },
23+
{ typeof(short), new HashSet<MemberInfo>() },
24+
{ typeof(ushort), new HashSet<MemberInfo>() },
25+
{ typeof(int), new HashSet<MemberInfo>() },
26+
{ typeof(uint), new HashSet<MemberInfo>() },
27+
{ typeof(long), new HashSet<MemberInfo>() },
28+
{ typeof(ulong), new HashSet<MemberInfo>() },
29+
{ typeof(float), new HashSet<MemberInfo>() },
30+
{ typeof(double), new HashSet<MemberInfo>() },
31+
{ typeof(decimal), new HashSet<MemberInfo>() },
32+
// { typeof(DateTime), new HashSet<MemberInfo>() },
33+
// { typeof(DateTimeOffset), new HashSet<MemberInfo>() },
34+
// { typeof(TimeSpan), new HashSet<MemberInfo>() },
35+
// { typeof(Guid), new HashSet<MemberInfo>() },
36+
// { typeof(Uri), new HashSet<MemberInfo>() },
37+
// { typeof(Enum), new HashSet<MemberInfo>() },
3738
#if NET6_0_OR_GREATER
38-
// { typeof(DateOnly), new HashSet<MethodInfo>() },
39-
// { typeof(TimeOnly), new HashSet<MethodInfo>() },
39+
// { typeof(DateOnly), new HashSet<MemberInfo>() },
40+
// { typeof(TimeOnly), new HashSet<MemberInfo>() },
4041
#endif
4142
};
4243

4344
public PredefinedMethodsHelper(ParsingConfig config)
4445
{
4546
foreach (var kvp in _supported)
4647
{
47-
Add(kvp.Key.GetMethod(nameof(Equals), _bindingFlags, null, [kvp.Key], null));
48-
Add(kvp.Key.GetMethod(nameof(Equals), _bindingFlags, null, [typeof(object)], null));
48+
Add(kvp.Key, ObjectInstanceEquals);
49+
Add(kvp.Key, kvp.Key.GetMethod(nameof(Equals), _bindingFlags, null, [kvp.Key], null));
50+
Add(kvp.Key, kvp.Key.GetMethod(nameof(Equals), _bindingFlags, null, [typeof(object)], null));
4951

50-
Add(kvp.Key.GetMethod(nameof(ToString), _bindingFlags, null, Type.EmptyTypes, null));
51-
Add(kvp.Key.GetMethod(nameof(ToString), _bindingFlags, null, [typeof(string)], null));
52-
Add(kvp.Key.GetMethod(nameof(ToString), _bindingFlags, null, [typeof(IFormatProvider)], null));
53-
Add(kvp.Key.GetMethod(nameof(ToString), _bindingFlags, null, [typeof(string), typeof(IFormatProvider)], null));
52+
Add(kvp.Key, ObjectInstanceToString);
53+
Add(kvp.Key, kvp.Key.GetMethod(nameof(ToString), _bindingFlags, null, Type.EmptyTypes, null));
54+
Add(kvp.Key, kvp.Key.GetMethod(nameof(ToString), _bindingFlags, null, [typeof(string)], null));
55+
Add(kvp.Key, kvp.Key.GetMethod(nameof(ToString), _bindingFlags, null, [typeof(IFormatProvider)], null));
56+
Add(kvp.Key, kvp.Key.GetMethod(nameof(ToString), _bindingFlags, null, [typeof(string), typeof(IFormatProvider)], null));
5457
}
5558

5659
if (config.AllowEqualsAndToStringMethodsOnObject)

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@ public void Method_ToString_OnDynamicLinq_And_SystemLinq_ShouldBeEqual()
1313
// Arrange
1414
var config = new ParsingConfig
1515
{
16-
AllowEqualsAndToStringMethodsOnObject = true
16+
// AllowEqualsAndToStringMethodsOnObject = true
1717
};
1818

19-
Expression<Func<int?, string?>> expr = x => x.ToString();
19+
Expression<Func<int, string>> expr = x => x.ToString();
2020

2121
var selector = "ToString()";
22-
var prm = Parameter(typeof(int?));
22+
var prm = Parameter(typeof(int));
2323
var parser = new ExpressionParser([prm], selector, [], config);
2424

2525
// Act

0 commit comments

Comments
 (0)