Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
...
  • Loading branch information
StefH committed Jan 29, 2025
commit 3101ebdb02523724c712d7327d3ce84451586adb
9 changes: 8 additions & 1 deletion src/System.Linq.Dynamic.Core/Parser/ExpressionParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public class ExpressionParser
private readonly ConstantExpressionHelper _constantExpressionHelper;
private readonly ITypeFinder _typeFinder;
private readonly ITypeConverterFactory _typeConverterFactory;
private readonly PredefinedMethodsHelper _predefinedMethodsHelper;
private readonly Dictionary<string, object> _internals = new();
private readonly Dictionary<string, object?> _symbols;
private readonly bool _usedForOrderBy;
Expand Down Expand Up @@ -99,6 +100,7 @@ public ExpressionParser(ParameterExpression[]? parameters, string expression, ob
_typeFinder = new TypeFinder(_parsingConfig, _keywordsHelper);
_typeConverterFactory = new TypeConverterFactory(_parsingConfig);
_constantExpressionHelper = ConstantExpressionHelperFactory.GetInstance(_parsingConfig);
_predefinedMethodsHelper = new PredefinedMethodsHelper(_parsingConfig);

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

case 1:
var method = (MethodInfo)methodBase!;
if (!PredefinedTypesHelper.IsPredefinedType(_parsingConfig, method.DeclaringType!) && !PredefinedMethodsHelper.IsPredefinedMethod(_parsingConfig, method))

int y = 0;
y.ToString();

if (!PredefinedTypesHelper.IsPredefinedType(_parsingConfig, method.DeclaringType!) &&
!_predefinedMethodsHelper.IsPredefinedMethod(type!, method))
{
throw ParseError(errorPos, Res.MethodIsInaccessible, id, TypeHelper.GetTypeName(method.DeclaringType!));
}
Expand Down
61 changes: 32 additions & 29 deletions src/System.Linq.Dynamic.Core/Parser/PredefinedMethodsHelper.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Linq.Dynamic.Core.Validation;
using System.Reflection;

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

private readonly Dictionary<Type, HashSet<MethodInfo>> _supported = new()
private readonly Dictionary<Type, HashSet<MemberInfo>> _supported = new()
{
{ typeof(bool), new HashSet<MethodInfo>() },
{ typeof(char), new HashSet<MethodInfo>() },
{ typeof(string), new HashSet<MethodInfo>() },
{ typeof(sbyte), new HashSet<MethodInfo>() },
{ typeof(byte), new HashSet<MethodInfo>() },
{ typeof(short), new HashSet<MethodInfo>() },
{ typeof(ushort), new HashSet<MethodInfo>() },
{ typeof(int), new HashSet<MethodInfo>() },
{ typeof(uint), new HashSet<MethodInfo>() },
{ typeof(long), new HashSet<MethodInfo>() },
{ typeof(ulong), new HashSet<MethodInfo>() },
{ typeof(float), new HashSet<MethodInfo>() },
{ typeof(double), new HashSet<MethodInfo>() },
{ typeof(decimal), new HashSet<MethodInfo>() },
// { typeof(DateTime), new HashSet<MethodInfo>() },
// { typeof(DateTimeOffset), new HashSet<MethodInfo>() },
// { typeof(TimeSpan), new HashSet<MethodInfo>() },
// { typeof(Guid), new HashSet<MethodInfo>() },
// { typeof(Uri), new HashSet<MethodInfo>() },
// { typeof(Enum), new HashSet<MethodInfo>() },
{ typeof(bool), new HashSet<MemberInfo>() },
{ typeof(char), new HashSet<MemberInfo>() },
{ typeof(string), new HashSet<MemberInfo>() },
{ typeof(sbyte), new HashSet<MemberInfo>() },
{ typeof(byte), new HashSet<MemberInfo>() },
{ typeof(short), new HashSet<MemberInfo>() },
{ typeof(ushort), new HashSet<MemberInfo>() },
{ typeof(int), new HashSet<MemberInfo>() },
{ typeof(uint), new HashSet<MemberInfo>() },
{ typeof(long), new HashSet<MemberInfo>() },
{ typeof(ulong), new HashSet<MemberInfo>() },
{ typeof(float), new HashSet<MemberInfo>() },
{ typeof(double), new HashSet<MemberInfo>() },
{ typeof(decimal), new HashSet<MemberInfo>() },
// { typeof(DateTime), new HashSet<MemberInfo>() },
// { typeof(DateTimeOffset), new HashSet<MemberInfo>() },
// { typeof(TimeSpan), new HashSet<MemberInfo>() },
// { typeof(Guid), new HashSet<MemberInfo>() },
// { typeof(Uri), new HashSet<MemberInfo>() },
// { typeof(Enum), new HashSet<MemberInfo>() },
#if NET6_0_OR_GREATER
// { typeof(DateOnly), new HashSet<MethodInfo>() },
// { typeof(TimeOnly), new HashSet<MethodInfo>() },
// { typeof(DateOnly), new HashSet<MemberInfo>() },
// { typeof(TimeOnly), new HashSet<MemberInfo>() },
#endif
};

public PredefinedMethodsHelper(ParsingConfig config)
{
foreach (var kvp in _supported)
{
Add(kvp.Key.GetMethod(nameof(Equals), _bindingFlags, null, [kvp.Key], null));
Add(kvp.Key.GetMethod(nameof(Equals), _bindingFlags, null, [typeof(object)], null));
Add(kvp.Key, ObjectInstanceEquals);
Add(kvp.Key, kvp.Key.GetMethod(nameof(Equals), _bindingFlags, null, [kvp.Key], null));
Add(kvp.Key, kvp.Key.GetMethod(nameof(Equals), _bindingFlags, null, [typeof(object)], null));

Add(kvp.Key.GetMethod(nameof(ToString), _bindingFlags, null, Type.EmptyTypes, null));
Add(kvp.Key.GetMethod(nameof(ToString), _bindingFlags, null, [typeof(string)], null));
Add(kvp.Key.GetMethod(nameof(ToString), _bindingFlags, null, [typeof(IFormatProvider)], null));
Add(kvp.Key.GetMethod(nameof(ToString), _bindingFlags, null, [typeof(string), typeof(IFormatProvider)], null));
Add(kvp.Key, ObjectInstanceToString);
Add(kvp.Key, kvp.Key.GetMethod(nameof(ToString), _bindingFlags, null, Type.EmptyTypes, null));
Add(kvp.Key, kvp.Key.GetMethod(nameof(ToString), _bindingFlags, null, [typeof(string)], null));
Add(kvp.Key, kvp.Key.GetMethod(nameof(ToString), _bindingFlags, null, [typeof(IFormatProvider)], null));
Add(kvp.Key, kvp.Key.GetMethod(nameof(ToString), _bindingFlags, null, [typeof(string), typeof(IFormatProvider)], null));
}

if (config.AllowEqualsAndToStringMethodsOnObject)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ public void Method_ToString_OnDynamicLinq_And_SystemLinq_ShouldBeEqual()
// Arrange
var config = new ParsingConfig
{
AllowEqualsAndToStringMethodsOnObject = true
// AllowEqualsAndToStringMethodsOnObject = true
};

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

var selector = "ToString()";
var prm = Parameter(typeof(int?));
var prm = Parameter(typeof(int));
var parser = new ExpressionParser([prm], selector, [], config);

// Act
Expand Down