栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

Express Tree表达式目录树

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Express Tree表达式目录树

表达式目录树以数据形式表示语言级别代码。数据存储在树形结构中。表达式目录树中的每个节点都表示一个表达式,例如一个方法调用或诸如 x < y 的二元运算。

下面的插图显示一个表达式及其表达式目录树形式的表示形式的示例。表达式的不同部分进行了颜色编码,以便与表达式目录树中相应的表达式目录树节点匹配。此外,还显示了不同类型的表达式目录树节点。


下面的代码示例演示如何将表示 lambda 表达式 num => num < 5 (C#) 或 Function(num) num < 5 (Visual Basic) 的表达式目录树分解为它的部分。

// Add the following using directive to your code file:
// using System.Linq.expressions;
// Create an expression tree.
expression> exprTree = num => num < 5;
// Decompose the expression tree.
Parameterexpression param = (Parameterexpression)exprTree.Parameters[0];
Binaryexpression operation = (Binaryexpression)exprTree.Body;
Parameterexpression left = (Parameterexpression)operation.Left;
Constantexpression right = (Constantexpression)operation.Right;
Console.WriteLine("Decomposed expression: {0} => {1} {2} {3}",
                  param.Name, left.Name, operation.NodeType, right.Value);



命名空间提供用于手动生成表达式目录树的 API。类包含创建特定类型的表达式目录树节点的静态工厂方法,例如,(表示一个已命名的参数表达式)或(表示一个方法调用)。命名空间中还定义了和其他特定于表达式的表达式目录树类型。这些类型派生自抽象类型

编译器也可以为您生成表达式目录树。编译器生成的表达式目录树的根始终在类型的节点中;也就是说,其根节点表示一个 lambda 表达式。

下面的代码示例演示创建表示 lambda 表达式 num => num < 5 (C#) 或 Function(num) num < 5 (Visual Basic) 的表达式目录树的两种方法。

// Add the following using directive to your code file:
// using System.Linq.expressions;
// Manually build the expression tree for 
// the lambda expression num => num < 5.
Parameterexpression numParam = expression.Parameter(typeof(int), "num");
Constantexpression five = expression.Constant(5, typeof(int));
Binaryexpression numLessThanFive = expression.LessThan(numParam, five);
expression> lambda1 =
    expression.Lambda>(
        numLessThanFive,
        new Parameterexpression[] { numParam });
// Let the compiler generate the expression tree for
// the lambda expression num => num < 5.
expression> lambda2 = num => num < 5;


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/233486.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号