mparlak / Flee

Fast Lightweight Expression Evaluator
630 stars 121 forks source link

Support delegation #102

Open syyqdsxfdmy opened 2 years ago

syyqdsxfdmy commented 2 years ago

I have a custom method to receive the delegation type. The method code is as follows: public static dynamic GREPARRAY(dynamic data, Func<dynamic, bool> func) { if (func==null) { func = item => item > 1; } List list = new List(); if (data is Array) { foreach (dynamic item in data) { if (func(item)) { list.Add(item); } } } return list; }

I try to use statements:“GREPARRAY(null,item=>item>1)”,An error occurred。 How do I call this method,thanks

OJB78 commented 1 year ago

Hello team!

It would be really great if we have support for delegation. I have the follow scenario. Is there any workaround to make this work? Thanks!

public class Test
{
    public int Method1(string str)
    {
        // Do something
        return 0;
    }

    public int Method2(string str)
    {
        // Do something different
        return 1;
    }

    public int RunTheMethod(Func<string,int> myMethodName)
    {
        // ... do stuff
        int i = myMethodName("Test");
        // ... do more stuff
        return i;
    }

    public void Init()
    {
        // This is working
        int x = RunTheMethod(Method1);
        int y = RunTheMethod(Method2);

        // This is not working
        ExpressionContext context = new ExpressionContext();
        IGenericExpression<int> e = context.CompileGeneric<int>("RunTheMethod(Method1)");
        int z = e.Evaluate();
    }
}