zzzprojects / Eval-Expression.NET

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

Execute fails on equation where "MyObject.Getter < MyMethod<MyType>(...)" #152

Closed MarkAD88 closed 10 months ago

MarkAD88 commented 10 months ago

The eval library (v5.0.12) fails if it tries to evaluate an equation that is like MyObject.Getter < MyMethod<MyType>().

The code below demonstrates the issue.

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

            // Less-than operator is the only one with issue
            // All other operators (==, !=, >) work just fine.

            // Works with a constant on the left
            System.Diagnostics.Debug.Assert(Z.Expressions.Eval.Execute<bool>("100 < GetValue<int>(\"200\")", helper) == true);

            // Works with the getter on the right
            System.Diagnostics.Debug.Assert(Z.Expressions.Eval.Execute<bool>("GetValue<int>(\"200\") < Thing.Value", helper) == false);

            // Works with the generic method wrapped in parenthesis
            System.Diagnostics.Debug.Assert(Z.Expressions.Eval.Execute<bool>("Thing.Value < (GetValue<int>(\"200\"))", helper) == true);

            // Works with the getter wrapped in parenthesis
            System.Diagnostics.Debug.Assert(Z.Expressions.Eval.Execute<bool>("(Thing.Value) < GetValue<int>(\"200\")", helper) == true);

            // Fails
            Z.Expressions.Eval.Execute<bool>("Thing.Value < GetValue<int>(\"200\")", helper);
        }
    }

    public class MyHelper
    {
        public TResult GetValue<TResult>(string input)
        {
            return (TResult) Convert.ChangeType(input, typeof(TResult));
        }

        public Thing Thing { get; set; } = new Thing();
    }

    public class Thing
    {
        public int Value = 100;
    }

Exception thrown is as follows:

Z.Expressions.Compiler.Shared.EvalException
  HResult=0x80131500
  Message=Oops! The current expression is null. The error occurred for expression "." at position 5 near ".Value < GetValue<int>(\"2".
  Source=Z.Expressions.Eval
  StackTrace:
   at .(Expression , SyntaxNode )
   at .(ExpressionScope , SyntaxNode , Func`3 )
   at .(ExpressionScope , SyntaxNode , Expression , Boolean )
   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 EvalErrorRepro.Program.Main(String[] args) in C:\Users\marka\source\repos\markad88\EvalErrorRepro\Program.cs:line 27
JonathanMagnan commented 10 months ago

Hello @MarkAD88 ,

Thank you for reporting. We will look at this issue.

Best Regards,

Jon

JonathanMagnan commented 10 months ago

Hello @MarkAD88 ,

The v6.0.2 has finally been released with this fix.

We improved in this version how we discover if an expression is a generic type or not.

Let me know if everything is now working as expected.

Best Regards,

Jon

MarkAD88 commented 10 months ago

After updating to v6.0.2 I can no longer reproduce the issue. Thank you.