Parsing Expression Trees C# with Example



Parsing Expression Trees C# with Example

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); 
// Decomposed expression: num => num LessThan 5 

0 Comment's

Comment Form

Submit Comment