使用C#防爆pression和或不包含EX pression一起根据AST [英] C# Expression using And Or and Not expression together based on AST

查看:215
本文介绍了使用C#防爆pression和或不包含EX pression一起根据AST的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用LINQ EX pression一些动态特性。我需要AND,OR和不EX pressions ..我提不起太大。

我们要检查的某些功能是否已启用或不符合我们的系统,并基于我们会决定是否显示菜单项或不。我们已经形成了以XML格式的规则,我所知道的转换规则,AST,但我不知道映射到LINQ EX pression。

规则是这样的:Feature1Enabled和Feature2Eenabled或(Feature3Disabled而不是Feature5Enabled)

下面Feature1Enabled,Feature2Eenabled等都是该功能的名称。我们将通过这个字符串IsFeatureEnabled功能来检查功能是否已启用与否。

 公共委托布尔IsEnabledDelegate(字符串名称,字符串值);

    公共静态布尔IsFeatureEnabled(字符串名称,字符串值)
    {
        如果(名称==MV)
            返回true;

        如果(名称==气和放大器;&安培;价值==G)
            返回true;
        //更多的条件去这里
        返回false;
    }

    静态无效的主要(字串[] args)
    {
        防爆pression< Func键<字符串,字符串,布尔>> featureEnabledExpTree =
                      (名称,值)=> IsFeatureEnabled(名称,值);

        //我要平等下面的语句在C#中的LINQ防爆pression

       布尔结果= IsFeatureEnabled(MV,)及&安培; IsFeatureEnabled(气,G)|| !IsFEatureEnabled(气,F)

    }
 

我想要的相当于      布尔结果= IsFeatureEnabled(MV,)及&安培; IsFeatureEnabled(气,G)|| !IsFEatureEnabled(气,F)

在LINQ的EX pression格式。不过,我可以将它们转换为动态的基础上我的AST符号。

感谢你这么多。如果您需要更多的信息,告诉我的意见。

解决方案

  ParameterEx pression名=前pression.Parameter(typeof运算(字符串),姓名) ,
        值=前pression.Parameter(typeof运算(字符串),价值);

    //构建逆
    防爆pression体=前pression.Constant(假);
    身体=前pression.Condition(
        防爆pression.AndAlso(
            防爆pression.Equal(姓名,防爆pression.Constant(气)),
            防爆pression.Equal(价值,防爆pression.Constant(G))
        ),防爆pression.Constant(真),身体);
    身体=前pression.Condition(
        防爆pression.Equal(姓名,防爆pression.Constant(MV)),
        防爆pression.Constant(真),身体);

    防爆pression< Func键<字符串,字符串,布尔>> featureEnabledExpTree =
        防爆pression.Lambda< Func键<字符串,字符串,布尔>>(身体,名称,价值);


    //测试隔离
    变种featureEnabledFunc = featureEnabledExpTree.Compile();
    布尔isMatch1 = featureEnabledFunc(MV,)
        &功放;&安培; featureEnabledFunc(气,G)|| !featureEnabledFunc(气,F);
 


然后,如果你需要的第二的部分为前pression树太:

  //我要平等下面的语句在C#中的LINQ防爆pression
    防爆pression< Func键<布尔>>测试=
        防爆pression.Lambda< Func键<布尔>>(
            防爆pression.OrElse(
                防爆pression.AndAlso(
                    防爆pression.Invoke(featureEnabledExpTree,
                        防爆pression.Constant(MV),
                        防爆pression.Constant()
                    ),
                    防爆pression.Invoke(featureEnabledExpTree,
                        防爆pression.Constant(气),
                        防爆pression.Constant(G)
                    )
                ),
                防爆pression.Not(
                    防爆pression.Invoke(featureEnabledExpTree,
                        防爆pression.Constant(气),
                        防爆pression.Constant(F)
                    )
                )
            )
        );

    布尔isMatch = test.Compile()();
 

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..

解决方案

    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()();

这篇关于使用C#防爆pression和或不包含EX pression一起根据AST的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆