为了账号安全,请及时绑定邮箱和手机立即绑定

请教如何分析表达式树

请教如何分析表达式树

一只甜甜圈 2018-12-06 13:09:27
MSDN官方示例:   // Add the following using directive to your code file:// using System.Linq.Expressions;// Create an expression tree.Expression<Func<int, bool>> 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);// This code produces the following output:// Decomposed expression: num => num LessThan 5 但现在的代码却运行不了,请高手指点:     public class Test { public void ExpressionTest() { T t=new T(){ID=1,Name="abc"}; Expression<Func<T, bool>> exprTree = o => o.ID == t.ID; ParameterExpression param = (ParameterExpression)exprTree .Parameters[0]; BinaryExpression operation = (BinaryExpression)exprTree.Body; MemberExpression left = (MemberExpression)operation.Left; MemberExpression right = (MemberExpression)operation.Right;//这里出错 Console.WriteLine("Decomposed expression:{0}=>{1}{2}{3}", param.Name,left.Member.Name,operation.NodeType,right.Value); } } public class T { public int ID { get; set; } public string Name { get; set; } }   MemberExpression right = (MemberExpression)operation.Right;//这一句想得到表达式右边的1,但程序总是出错,请高手指点
查看完整描述

1 回答

  • 1 回答
  • 0 关注
  • 283 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信