基于 AST 结合使用 And Or 和 Not 表达式的 C# 表达式

发布于 2024-08-01 15:08:26 字数 1348 浏览 1 评论 0原文

我想使用 Linq 表达式来实现一些动态功能。 我需要 And、Or 和 Not 表达式.. 我无法得到太多..

我们想要检查我们的系统中是否启用了某些功能,并基于此我们将决定是否显示菜单项。 我们已经形成了 XML 格式的规则,我知道将规则转换为 AST,但我不知道映射到 Linq 表达式。

规则如下:Feature1Enabled 和Feature2Eenabled 或(Feature3Disabled 且不是Feature5Enabled)

这里“Feature1Enabled”、“Feature2Eenabled”等是功能的名称。 我们将此字符串传递给 IsFeatureEnabled 函数来检查某个功能是否已启用。

  public delegate bool IsEnabledDelegate(string name, string value);

    public static bool IsFeatureEnabled(string name, string value)
    {
        if (name == "MV")
            return true;

        if (name == "GAS" && value == "G")
            return true;
        //More conditions goes here
        return false;
    }

    static void Main(string[] args)
    {
        Expression<Func<string, string, bool>> featureEnabledExpTree = 
                      (name, value) => IsFeatureEnabled(name, value);

        //I want the equal of the following statement in C# Linq Expression

       bool result = IsFeatureEnabled("MV", "") && IsFeatureEnabled("GAS", "G") || !IsFEatureEnabled("GAS", "F")

    }

我想要相当于 bool 结果 = IsFeatureEnabled("MV", "") && IsFeatureEnabled("GAS", "G") || Linq 表达式格式中的!IsFEatureEnabled("GAS", "F")

.. 但是我可以根据我的 AST 符号将它们动态转换..

非常感谢.. 如果您需要更多信息,请在评论中告诉我..

I want to use Linq expression for some dynamic features. I need And, Or and Not expressions.. I couldn't get much..

We want to check whether certain feature has been enabled or not in our system and based on that we will decide whether to show the menu item or not. we have formed rules in XML format, I know to convert the rule to AST but I don't know to map to Linq expression.

Rules Are like : Feature1Enabled And Feature2Eenabled or (Feature3Disabled And Not Feature5Enabled)

Here "Feature1Enabled", "Feature2Eenabled" etc are name of the feature. We will pass this string to IsFeatureEnabled function to check whether a feature has been enabled or not.

  public delegate bool IsEnabledDelegate(string name, string value);

    public static bool IsFeatureEnabled(string name, string value)
    {
        if (name == "MV")
            return true;

        if (name == "GAS" && value == "G")
            return true;
        //More conditions goes here
        return false;
    }

    static void Main(string[] args)
    {
        Expression<Func<string, string, bool>> featureEnabledExpTree = 
                      (name, value) => IsFeatureEnabled(name, value);

        //I want the equal of the following statement in C# Linq Expression

       bool result = IsFeatureEnabled("MV", "") && IsFeatureEnabled("GAS", "G") || !IsFEatureEnabled("GAS", "F")

    }

I want the equivalent to the
bool result = IsFeatureEnabled("MV", "") && IsFeatureEnabled("GAS", "G") || !IsFEatureEnabled("GAS", "F")

in Linq expression Format.. However I can convert them to dynamically based on my AST notations..

Thank you so much.. If you need more info, tell me in comments..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

青衫负雪 2024-08-08 15:08:26

像那样?

Expression<Func<bool>> featureEnabledExpTree = () =>
    IsFeatureEnabled("MV", "") &&
    IsFeatureEnabled("GAS", "G") ||
    !IsFEatureEnabled("GAS", "F");

like that?

Expression<Func<bool>> featureEnabledExpTree = () =>
    IsFeatureEnabled("MV", "") &&
    IsFeatureEnabled("GAS", "G") ||
    !IsFEatureEnabled("GAS", "F");
为你拒绝所有暧昧 2024-08-08 15:08:26
    ParameterExpression name = Expression.Parameter(typeof(string), "name"),
        value = Expression.Parameter(typeof(string), "value");

    // build in reverse
    Expression body = Expression.Constant(false);
    body = Expression.Condition(
        Expression.AndAlso(
            Expression.Equal(name, Expression.Constant("GAS")),
            Expression.Equal(value, Expression.Constant("G"))
        ), Expression.Constant(true), body);
    body = Expression.Condition(
        Expression.Equal(name, Expression.Constant("MV")),
        Expression.Constant(true), body);

    Expression<Func<string, string, bool>> featureEnabledExpTree =
        Expression.Lambda<Func<string, string, bool>>(body, name, value);


    // test in isolation
    var featureEnabledFunc = featureEnabledExpTree.Compile();
    bool isMatch1 = featureEnabledFunc("MV", "")
        && featureEnabledFunc("GAS", "G") || !featureEnabledFunc("GAS", "F");

然后,如果您也需要第二部分作为表达式树:

    //I want the equal of the following statement in C# Linq Expression
    Expression<Func<bool>> test =
        Expression.Lambda<Func<bool>>(
            Expression.OrElse(
                Expression.AndAlso(
                    Expression.Invoke(featureEnabledExpTree,
                        Expression.Constant("MV"),
                        Expression.Constant("")
                    ),
                    Expression.Invoke(featureEnabledExpTree,
                        Expression.Constant("GAS"),
                        Expression.Constant("G")
                    )
                ),
                Expression.Not(
                    Expression.Invoke(featureEnabledExpTree,
                        Expression.Constant("GAS"),
                        Expression.Constant("F")
                    )
                )
            )
        );

    bool isMatch = test.Compile()();
    ParameterExpression name = Expression.Parameter(typeof(string), "name"),
        value = Expression.Parameter(typeof(string), "value");

    // build in reverse
    Expression body = Expression.Constant(false);
    body = Expression.Condition(
        Expression.AndAlso(
            Expression.Equal(name, Expression.Constant("GAS")),
            Expression.Equal(value, Expression.Constant("G"))
        ), Expression.Constant(true), body);
    body = Expression.Condition(
        Expression.Equal(name, Expression.Constant("MV")),
        Expression.Constant(true), body);

    Expression<Func<string, string, bool>> featureEnabledExpTree =
        Expression.Lambda<Func<string, string, bool>>(body, name, value);


    // test in isolation
    var featureEnabledFunc = featureEnabledExpTree.Compile();
    bool isMatch1 = featureEnabledFunc("MV", "")
        && featureEnabledFunc("GAS", "G") || !featureEnabledFunc("GAS", "F");

And then, if you need the second portion as an expression tree too:

    //I want the equal of the following statement in C# Linq Expression
    Expression<Func<bool>> test =
        Expression.Lambda<Func<bool>>(
            Expression.OrElse(
                Expression.AndAlso(
                    Expression.Invoke(featureEnabledExpTree,
                        Expression.Constant("MV"),
                        Expression.Constant("")
                    ),
                    Expression.Invoke(featureEnabledExpTree,
                        Expression.Constant("GAS"),
                        Expression.Constant("G")
                    )
                ),
                Expression.Not(
                    Expression.Invoke(featureEnabledExpTree,
                        Expression.Constant("GAS"),
                        Expression.Constant("F")
                    )
                )
            )
        );

    bool isMatch = test.Compile()();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文