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

Compile Expression expecting an Expando Throws #136

Open tcpmod opened 2 years ago

tcpmod commented 2 years ago

Hello, When I use execute against the following expression string on the expandoObject it works nicely, returns bool (true).

However when I compile the expression and try to invoke the Func<ExpandObject,bool> it throws with the exception below.

Would be grateful to know if compile and invoke would be expected to work in this scenario, considering Execute does.

Many thanks in advance. (example against .net 6.0)

dynamic input = new ExpandoObject();
        input.User = new {
            Addresses = new[]{
                new{
                    Type = "Shipping",
                    Line1 = "York St"
                },
                new{
                    Type = "Billing",
                    Line1 = "Fleet St"
                }
            }
        };

        var exprString1 = "User.Addresses.FirstOrDefault(a => a.Type == \"Shipping\")?.Line1 == \"York St\"";

        /* this works very nicely - returns true */
        var execWorks = Eval.Execute<bool>(exprString1, input);

        /* this compiles without error but invocation fails */
        Func<ExpandoObject,bool> compiled = Eval.Compile<Func<ExpandoObject,bool>>(exprString1);
        var result = compiled(input);

exception thrown

System.Exception
  HResult=0x80131500
  Message=Oops! A parameter still have the type 'SyntaxNode'
  Source=Z.Expressions.Eval
  StackTrace:
   at .DynamicMethod(Object obj, Boolean isExtensionMethod, Object betterMember, String name, Object[] parameters, List`1 parameterExpressionInfos, List`1 extensionMethodRegister)
   at System.Dynamic.UpdateDelegates.UpdateAndExecute2[T0,T1,TRet](CallSite site, T0 arg0, T1 arg1)
JonathanMagnan commented 2 years ago

Hello @tcpmod ,

Thank you for reporting, we will look at this.

The Execute method can work as we know the type of the Addresses (Even if we compile the method, we can still have access to the value of the parameter).

The Compile method currently doesn't work as we probably have no idea what is the current type. Since that's an ExpandoObject, the type could be anything.

I will assign my developer to look at what can be done here.

Best Regards,

Jon

tcpmod commented 2 years ago

Many thanks @JonathanMagnan for the update, yes that makes sense. Kind regards Mike.