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

Eval.Execute Error - Oops! No applicable member has been found for the expression. #67

Closed se closed 4 years ago

se commented 4 years ago

Hi,

First of all, thank you for this awesome project (like others you have made!).

We are having issues with nested dynamic objects with functions. I'm gonna leave the example here and also dotnet fiddle URL to test it fast.

Dotnet Fiddle;

https://dotnetfiddle.net/XXtvCq

Is this a bug or is there anything we can do to make it work?

Example Code;

// @nuget: Z.Expressions.Eval

using System;
using Z.Expressions;
using System.Dynamic;

public class Program
{
    public static void Main()
    {
        var fSum = (Func<int, int, object>)((v1, v2)=> v1+v2);
        var fSub = (Func<int, int, object>)((v1, v2)=> v1-v2);
        var fMult = (Func<int, int, object>)((v1, v2)=> v1*v2);
        var fDiv = (Func<int, int, object>)((v1, v2)=> v1/v2);

        dynamic functions = new ExpandoObject();
        functions.Sum = fSum;
        functions.Substract = fSub;
        functions.Multiply = fMult;
        functions.Divide = fDiv;

        dynamic expando = new ExpandoObject();
        expando.ObjectId = Guid.NewGuid();

        // These methods are working
        expando.Sum = fSum;
        expando.Substract = fSub;
        expando.Multiply = fMult;
        expando.Divide = fDiv;

        // But these are not!
        expando.Functions = functions;

        var context = new EvalContext();
        // Working Example
        Console.WriteLine(context.Execute<object>("Sum(1,5)", expando));
        // Not Working Example
        Console.WriteLine(context.Execute<object>("Functions.Sum(1,5)", expando));
    }
}
JonathanMagnan commented 4 years ago

Hello @se ,

Thank you for reporting,

That's an interesting one ;)

Best Regards,

Jon

se commented 4 years ago

Is there anything we can do? 🤔

JonathanMagnan commented 4 years ago

Not right now,

One of my employees is currently looking at it. So I should be able to give you an update very soon.

JonathanMagnan commented 4 years ago

Hello @se ,

The v3.0.13 has been released.

Could you try it and confirm that everything is now working?

se commented 4 years ago

It is working fine! Thank you so much!