zzzprojects / Eval-Expression.NET

C# Eval Expression | Evaluate, Compile, and Execute C# code and expression at runtime.
https://eval-expression.net/
Other
459 stars 86 forks source link

Executing code that calls a method with a params list fails if the last value provided is a boolean. #151

Closed MarkAD88 closed 1 year ago

MarkAD88 commented 1 year ago

The eval library (v5.0.11) seems to have a problem when executing code that calls a method that supports a params list but only if the last value in the param list is a boolean. I've tested on both .NET 6 and .NET 7 and the results are the same.

The code below demonstrates the issue.

    internal class Program
    {
        static void Main(string[] args)
        {
            var helper = new MyHelper();

            // TestMethod works with arbitrary number of parameters even if they are booleans
            System.Diagnostics.Debug.Assert(Z.Expressions.Eval.Execute<int>("TestMethod(\"success\", false, false, 1)", helper) == 3);
            System.Diagnostics.Debug.Assert(Z.Expressions.Eval.Execute<int>("TestMethod(\"success\", \"one\")", helper) == 1);

            // Following lines will error with TestMethod not found if a boolean is the last parameter
            Z.Expressions.Eval.Execute<int>("TestMethod(\"fail\", 1, 2, 3, false)", helper);
            Z.Expressions.Eval.Execute<int>("TestMethod(\"fail\", false)", helper);
        }
    }

    public class MyHelper
    {
        public int TestMethod(string name, params object[] args)
        {
            return args.Length;
        }
    }

The exception thrown is as follows:

Z.Expressions.Compiler.Shared.EvalException
  HResult=0x80131500
  Message=Oops! No applicable member has been found for the expression. The error occurred for expression "TestMethod" at position 0 near "TestMethod(\"fail\", 1, 2, ".
  Source=Z.Expressions.Eval
  StackTrace:
   at .(ExpressionScope , Expression , Type , String , List`1 , SyntaxNode , List`1 , Boolean )
   at .(ExpressionScope , Expression , Type , String , List`1 , Boolean , List`1 , SyntaxNode , Expression& )
   at .(ExpressionScope , SyntaxNode , Type , Expression , List`1 )
   at .(ExpressionScope , SyntaxNode , Expression , Boolean )
   at .(ExpressionScope , SyntaxNode , Expression , Boolean )
   at Z.Expressions.CodeCompiler.CSharp.ExpressionParser.ParseSyntax(ExpressionScope scope, SyntaxNode node, Type resultType)
   at .[](EvalContext , String , IDictionary`2 , Type , EvalCompilerParameterKind , ExpressionScope , String , Boolean , Boolean )
   at .[](EvalContext , String , IDictionary`2 , Type , EvalCompilerParameterKind , Boolean , Boolean , Boolean )
   at Z.Expressions.EvalContext.Execute[TResult](String code, Object parameters)
   at Z.Expressions.Eval.Execute[TResult](String code, Object parameters)
   at EvalErrorRepro.Program.Main(String[] args) in C:\Users\marka\source\repos\markad88\EvalErrorRepro\Program.cs:line 14
JonathanMagnan commented 1 year ago

Hello @MarkAD88 ,

Thank you for reporting, my developer will look at it.

Best Regards,

Jon

JonathanMagnan commented 1 year ago

Hello @MarkAD88 ,

Just to let you know that a fix has been merged in our master.

It should be released either on November 7th or November 14th.

Best Regards,

Jon

JonathanMagnan commented 1 year ago

Hello @MarkAD88 ,

The v5.0.12 has been released today.

Is it possible for you to double-check and let us know if my developer fixed this issue correctly?

Best Regards,

Jon

MarkAD88 commented 1 year ago

@JonathanMagnan,

I tested v5.0.12 with both my repro code and with a larger project where I originally found the bug. I can no longer reproduce the issue. Thank you for the prompt turnaround on the fix.