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

Compile Expression expecting an Expando Throws #136

Open
tcpmod opened this issue Jun 19, 2022 · 2 comments
Open

Compile Expression expecting an Expando Throws #136

tcpmod opened this issue Jun 19, 2022 · 2 comments
Assignees

Comments

@tcpmod
Copy link

tcpmod commented Jun 19, 2022

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 JonathanMagnan self-assigned this Jun 20, 2022
@JonathanMagnan
Copy link
Member

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
Copy link
Author

tcpmod commented Jun 20, 2022

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants