基于 AST 结合使用 And Or 和 Not 表达式的 C# 表达式
我想使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
像那样?
like that?
然后,如果您也需要第二部分作为表达式树:
And then, if you need the second portion as an expression tree too: