Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用BinaryExpression和_C#_Functional Programming_Expression Trees - Fatal编程技术网

C# 使用BinaryExpression和

C# 使用BinaryExpression和,c#,functional-programming,expression-trees,C#,Functional Programming,Expression Trees,我只是在学习表达式树的早期过程中 据我所知,它们的优点在于,当它们被用作参数时,可以对其进行解析,例如: Foo.Bar(x => x.Process == "On" && x.Name == "Goofy") 但是,当有和在内部时,如何解析表达式 也许我完全误解了,但我看不出使用表达式的原因?我现在查看了近数百个网站,它们都试图解释表达式树是什么,但都成功了,除了那些无关紧要的解释“表达式树不是编译的…”之外,它们从未解释过它的用途。我使用表达式构建了一种无关紧要的领域

我只是在学习表达式树的早期过程中

据我所知,它们的优点在于,当它们被用作参数时,可以对其进行解析,例如:

Foo.Bar(x => x.Process == "On" && x.Name == "Goofy")
但是,当有和在内部时,如何解析表达式


也许我完全误解了,但我看不出使用表达式的原因?我现在查看了近数百个网站,它们都试图解释表达式树是什么,但都成功了,除了那些无关紧要的解释“表达式树不是编译的…”之外,它们从未解释过它的用途。

我使用表达式构建了一种无关紧要的领域特定语言。这是在我工作的实际应用程序中使用的,所以它不是一个玩具项目

我基本上创建了一个流畅的界面,允许我构建
谓词
委托,比如:

Predicate<MyClass> condition = (new ConditionBuilder()).GreaterThanConst(9)
                                                       .And()
                                                       .LessThanConst(4)
                                                       .ToPredicate();
public ConditionBuilder GreaterThan(object constant)
{
    condition = Expression.GreaterThan(Expression.Parameter(constant.GetType()), Expression.Constant(constant));
    return this;
}
Expression<Func<YourClass, bool>> expr = x => x.Process == "On" && x.Name == "Goofy";
其中,
condition
是保存正在构建(生长?)的树的成员

要解析表达式,您需要编写如下内容:

Predicate<MyClass> condition = (new ConditionBuilder()).GreaterThanConst(9)
                                                       .And()
                                                       .LessThanConst(4)
                                                       .ToPredicate();
public ConditionBuilder GreaterThan(object constant)
{
    condition = Expression.GreaterThan(Expression.Parameter(constant.GetType()), Expression.Constant(constant));
    return this;
}
Expression<Func<YourClass, bool>> expr = x => x.Process == "On" && x.Name == "Goofy";
expr=x=>x.Process==“On”&&x.Name==“Goofy”;

然后,您可以查看生成的表达式树并查看其中的内容。

表达式以树的形式表示代码。树的每个节点表示代码的一部分。例如,问题中lambda表达式主体的树是这样的:

BinaryExpression (AndAlso)
  .Left = BinaryExpression (Equal)
    .Left = MemberExpression (x.Process)
      .Expression = ParameterExpression (x)
      .Member = MemberInfo (Process)
    .Right = ConstantExpression ("On")
  .Right = BinaryExpression (Equal)
    .Left = MemberExpression (x.Name)
      .Expression = ParameterExpression (x)
      .Member = MemberInfo (Name)
    .Right = ConstantExpression ("Goofy")

通常,表达式表示的代码不执行,甚至不编译:对其进行分析以从中提取一些信息,将其转换为另一个表达式,甚至生成完全不同的内容(如Linq to SQL或实体框架中的SQL查询)。对于编译后的代码,这是不可能的,因为无法分析它(至少不容易)。

感谢您澄清这一点,这正是我所希望的:)它确实回答了我的最后一个问题。谢谢您。。。是的,我确实想象过类似于扩展公共静态boolbar(这个Foo-Foo,Expression expr){…}的东西,然后查看表达式树,计算false或true,然后返回。我走对了吗?我不知道你为什么需要一个表达式树。如果您只想返回结果,那么只需传入
Func
本身就更有意义了。我受到以下启发:NServiceBus.Configure.With().Log4Net(a=>a.YourProperty=“value”);设置如下属性:)