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

Error when call .Count() with Dictionary<string, object> #80

Closed cuiliang closed 4 years ago

cuiliang commented 4 years ago

Code sample: https://dotnetfiddle.net/N2GCak

using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using Z.Expressions;

public class Program
{
    public static void Main()
    {

            var evalContext = new EvalContext();

            IList<string> list = new List<string>() { "aaa", "bbb", "ccc" };
            var dict = new Dictionary<string, object>() { { "A", "AAAA" }, { "B", "BBBB" } };

            evalContext.RegisterGlobalVariable("list", list);
            evalContext.RegisterGlobalVariable("dict", dict);

            Console.WriteLine("list count:" + evalContext.Execute("list.Count()").ToString()); // ok with list

            Console.WriteLine("dict count:" + dict.Count());
            Console.WriteLine("dict count:" + evalContext.Execute("dict.Count()").ToString());  // error with dictionary
    }
}

Output:

list count:3
dict count:2
Run-time exception (line 26): Oops! No applicable member has been found for the expression. The error occurred for expression "." at position 4 near ".Count()".

Stack Trace:

[System.Exception: Oops! No applicable member has been found for the expression. The error occurred for expression "." at position 4 near ".Count()".]
   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 .(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 )
   at Z.Expressions.EvalContext.Execute[TResult](String code)
   at Z.Expressions.EvalContext.Execute(String code)
   at Program.Main() :line 26
JonathanMagnan commented 4 years ago

Hello @cuiliang ,

Thank you for reporting, we will look at it and fix it.

Best Regards,

Jon

cuiliang commented 4 years ago

version 4.0.4 does not solve this problem.

JonathanMagnan commented 4 years ago

You are right,

The fix for your issue is still under pending review as some core code has been modified and we didn't find the time to complete the code review for the v4.0.4

It should be done early this week.

JonathanMagnan commented 4 years ago

Hello @cuiliang ,

The v4.0.5 has been released.

The fix for your issue has been merged for this version.

Could you try it and let us know if everything is working.

Best Regards,

Jon

cuiliang commented 4 years ago

Yes! thankyou.