3

I'm trying to parse string to create Func instance.

public class A
{
    public int fieldA { get; private set; }
    public bool fieldB { get; private set; }
    public List<int> fieldC { get; private set; }
}

Check the condition of the instance of A class with :

{
    "Type" : "A"
    "Condition" : "fieldA > 0 && fieldB == true"
}

Of course the format of a string can be modified, for easier parsing.

What I want to create is actually :

Func<A, bool> condition = a => a.fieldA > 0 && a.fieldB == true;

Parsing the condition string would not be very hard, but since there are so many A instances, using reflection whenever checking the condition would not be desirable. What I want to do is "construct" this function from string in advance and cache it somewhere, and call cached function when I want to check the condition.

Could you give my any advices?

10
  • I think there's a "dynamic LINQ" library, which might do what you want. I don't think there are any shortcuts in the standard .NET libraries themselves though. Jul 28, 2020 at 5:54
  • I would use (CSScript)[github.com/oleg-shilo/cs-script] to compile the condition. Jul 28, 2020 at 5:56
  • @LegacyCode your link is broken unfortunately mate. It says "404 this is not the page you are looking for"
    – Mong Zhu
    Jul 28, 2020 at 6:03
  • How do you obtain the condition? Is it a constant and present from the start of the program or do you get it as some input from network etc.?
    – Ackdari
    Jul 28, 2020 at 6:08
  • 1
    @Mong Zhu - Sorry the link was not correctly formatted github.com/oleg-shilo/cs-script Jul 28, 2020 at 11:42

1 Answer 1

9

Using System.Linq.Dynamic.Core you could do something like:

public bool TestCondition(A obj, string condition)
{
    var expr = DynamicExpressionParser.ParseLambda<A, bool>(ParsingConfig.Default, false, condition, new object[0]);
    var func = expr.Compile();
    return func(obj);
}

And invoke it like:

var testResult = TestCondition(anInstanceOfA, "fieldA > 0 && fieldB == true");

If you want to use local variables in your expression you can do:

public static bool TestCondition(A obj, string condition, IDictionary<string, object> args)
{
    var expr = DynamicExpressionParser.ParseLambda(new ParameterExpression[] { Expression.Parameter(typeof(A)) },
        typeof(bool),
        condition,
        new object[] { args });

    var func = expr.Compile();
    return (bool)func.DynamicInvoke(obj);
}

And invoke it like:

var locals = new Dictionary<string, object>();
locals.Add("myInt", 0);
locals.Add("myBool", true);
var check = TestCondition(obj, "fieldA > myInt && fieldB == myBool", locals);

You can also make the method generic so you can reuse it in other circumstances:

public static TOut TestCondition<TIn, TOut>(TIn obj, string condition, IDictionary<string, object> args)
{
    var expr = DynamicExpressionParser.ParseLambda(new ParameterExpression[] { Expression.Parameter(typeof(TIn)) },
        typeof(TOut),
        condition,
        new object[] { args });

    var func = expr.Compile();
    return (TOut)func.DynamicInvoke(obj);
}

.
.
.
TestCondition<A, bool>(obj, "fieldA > myInt && fieldB == myBool", locals)
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.