DynamicLinq : how to use custom class method without class name to ParseLambda

449 Views Asked by At

I'm using DynamicExpressionParser.ParseLambda(ParsingConfig, ....)

which excepts ParsingConfig where we can specify custom class to use its method in expression, which can be parsed as valid LambdaExpression

I've registered custom static class in ParsingConfig by CustomTypeProvider and can parse lambda expression as well.

but I need to create expression as StaticClassName.Method(input)

Is there any way where I can register my static class methods so I can call method directly directly without specifying class name? like Method(input) in expression?

As DynamicLinq support many custom functions like iif(x, y, z) which are available to use in expression directly as inbuilt method

Reference: https://dynamic-linq.net/expression-language

so was thinking if it provide any way to register custom class method, by which we can just use method name in expression or not?

Please let me know if there is any other way with/without DynamicLinq which allows such functionality.

1

There are 1 best solutions below

1
On

Did you annotate your static class with the [DynamicLinqType] attribute?

Example:

[DynamicLinqType]
public static class Utils
{
    public static int ParseAsInt(string value)
    {
        if (value == null)
        {
             return 0;
        }

        return int.Parse(value);
    }

    public static int IncrementMe(this int values)
    {
        return values + 1;
    }
}

This is needed so that System.Linq.Dynamic.Core can handle these custom classes.

See also https://dynamic-linq.net/advanced-extending