Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DynamicExpressionParser.ParseLambda can not parse 'TrimEnd' string method #487

Closed
Evgentus0 opened this issue Feb 22, 2021 · 3 comments
Closed
Labels

Comments

@Evgentus0
Copy link

Here is what to include in your request to make sure we implement a solution as quickly as possible.

1. Description

Trying parse expression which using 'TrimEnd' string method I get an error "No applicable method 'TrimEnd' exists in type 'String''"

2. Exception

  • Message : "No applicable method 'TrimEnd' exists in type 'String'"
  • StackTrace:

    at System.Linq.Dynamic.Core.Parser.ExpressionParser.ParseMemberAccess(Type type, Expression expression)
    at System.Linq.Dynamic.Core.Parser.ExpressionParser.ParseIdentifier()
    at System.Linq.Dynamic.Core.Parser.ExpressionParser.ParsePrimaryStart()
    at System.Linq.Dynamic.Core.Parser.ExpressionParser.ParsePrimary()
    at System.Linq.Dynamic.Core.Parser.ExpressionParser.ParseUnary()
    at System.Linq.Dynamic.Core.Parser.ExpressionParser.ParseMultiplicative()
    at System.Linq.Dynamic.Core.Parser.ExpressionParser.ParseAdditive()
    at System.Linq.Dynamic.Core.Parser.ExpressionParser.ParseShiftOperator()
    at System.Linq.Dynamic.Core.Parser.ExpressionParser.ParseComparisonOperator()
    at System.Linq.Dynamic.Core.Parser.ExpressionParser.ParseLogicalAndOrOperator()
    at System.Linq.Dynamic.Core.Parser.ExpressionParser.ParseIn()
    at System.Linq.Dynamic.Core.Parser.ExpressionParser.ParseAndOperator()
    at System.Linq.Dynamic.Core.Parser.ExpressionParser.ParseOrOperator()
    at System.Linq.Dynamic.Core.Parser.ExpressionParser.ParseLambdaOperator()
    at System.Linq.Dynamic.Core.Parser.ExpressionParser.ParseNullCoalescingOperator()
    at System.Linq.Dynamic.Core.Parser.ExpressionParser.ParseConditionalOperator()
    at System.Linq.Dynamic.Core.Parser.ExpressionParser.Parse(Type resultType, Boolean createParameterCtor)
    at System.Linq.Dynamic.Core.DynamicExpressionParser.ParseLambda(Type delegateType, ParsingConfig parsingConfig, Boolean createParameterCtor, ParameterExpression[] parameters, Type resultType, String expression, Object[] values)
    at System.Linq.Dynamic.Core.DynamicExpressionParser.ParseLambda(ParsingConfig parsingConfig, Boolean createParameterCtor, ParameterExpression[] parameters, Type resultType, String expression, Object[] values)
    at System.Linq.Dynamic.Core.DynamicExpressionParser.ParseLambda[T,TResult](ParsingConfig parsingConfig, Boolean createParameterCtor, String expression, Object[] values)
    at TestEfTranslator.Program.Main(String[] args) in D:\Projects\Examples\TestEfTranslator\TestEfTranslator\Program.cs:line 32

3. Fiddle or Project

To reproduce this issue you can just try to parse next expression:

var expression = DynamicExpressionParser.ParseLambda<string, bool>(new ParsingConfig(), false, "TrimEnd().EndsWith(@0)", "test");
You may even do not use parameters, the main thing is 'TrimEnd()'

@Evgentus0
Copy link
Author

It seems that the reason is in MethodFinder.cs, method FindMethod (line 28). The line MemberInfo[] members = t.FindMembers(MemberTypes.Method, flags, Type.FilterNameIgnoreCase, methodName); with method name 'TrimEnd' return only 1 found method, TrimEnd(char[]), so if use TrimEnd() without parameters it will parse in incorrect way.
Try to run something like DynamicExpressionParser.ParseLambda<string, string>(new ParsingConfig(), false, "TrimEnd()"); and you will see expression with strange (null) parameter

@AndriiZ
Copy link
Contributor

AndriiZ commented Feb 22, 2021

Solution:
add
if (args.Length == 0)
{
better = (first.Parameters.Length == 0) && (second.Parameters.Length != 0);
}
to the beginning of bool IsBetterThan(Expression[] args, MethodData first, MethodData second)

Looks like it is lib cant make a dicision if TrimEnd() is better than TrimEnd(char)

@StefH
Copy link
Collaborator

StefH commented Feb 22, 2021

Thanks for the fix.

It's merged and I'll add some unit tests.

@StefH StefH closed this as completed Feb 22, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

3 participants