Expression.Reduce()有什么作用? [英] What does Expression.Reduce() do?

查看:107
本文介绍了Expression.Reduce()有什么作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用表达式树几天了,我很好奇知道Expression.Reduce()的作用。 msdn文档并不是很有帮助,因为它仅声明它减少了表达式。为了以防万一,我尝试了一个示例(请参见下文),以检查此方法是否包含数学归约法,但事实并非如此。

I've been working with expression trees for a few days now and I'm curious to know what Expression.Reduce() does. The msdn documentation is not very helpful as it only states that it "reduces" the expression. Just in case, I tried an example (see below) to check if this method included mathematical reduction, but this doesn't seem to be the case.

有人知道吗此方法的作用是什么,是否有可能提供一个快速的示例演示该方法的实际作用?

Does anyone know what this method does and is it possible to provide a quick example showing it in action? Any good resources out there?

static void Main(string[] args)
{
    Expression<Func<double, double>> func = x => (x + x + x) + Math.Exp(x + x + x);
    Console.WriteLine(func);
    Expression r_func = func.Reduce();
    Console.WriteLine(r_func); // This prints out the same as Console.WriteLine(func)
}


推荐答案

您需要查看的文档是 expr-tree-spec.doc

The document you need to look at is expr-tree-spec.doc.

这是表达式树的规范。阅读 2.2可还原节点和 4.3.5缩减方法部分。

This is the specification for the expression trees. Read the "2.2 Reducible Nodes" and "4.3.5 Reduce Method" sections.

基本上,此方法供实现或将动态语言移植到.NET的人员使用。这样他们就可以创建自己的节点,这些节点可以缩减为标准的表达式树节点并可以进行编译。表达式树API中有一些可还原节点,但是我不知道您是否可以获得任何实际示例(因为所有标准表达式节点都可以编译,作为最终用户,您可能不在乎是否被还原了)是

Basically, this method is intended for people implementing or porting their dynamic langauges to .NET. So that they can create their own nodes that can "reduce" to standard expression tree nodes and can be compiled. There are some "reducible" nodes in the expression trees API, but I don't know whether you can get any practical examples (since all standard expression nodes compile anyway, as the end-user you probably do not care whether they are "reduced" behind the scenes or not).

是的,MSDN文档在这方面非常基础,因为语言实现者的主要信息和文档来源是 http://dlr.codeplex.com/

Yes, MSDN documentation is very basic in this area, because the main source of info and docs for language implementers is http://dlr.codeplex.com/

这篇关于Expression.Reduce()有什么作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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