private void Test1() { var ParsingExpressionTrees = new ParsingExpressionTrees(); var ParasingExpressionTreeByVisitor = new ParsingExpressionTreeByVisitor(); { ParameterExpression valueExp = Expression.Parameter(typeof(int), "Value"); //创建参数表达式 Value ParameterExpression resultExp = Expression.Parameter(typeof(int), "Result"); //创建参数表达式 Result LabelTarget lableExp = Expression.Label(typeof(int), "Break"); //创建一个标签以从循环跳转到,接收一个int类型的值 BlockExpression blockExp = Expression.Block( //创建代码块 new[] { resultExp } //定义局部变量 , Expression.Assign(resultExp, Expression.Constant(1)) //给变量赋值 , Expression.Loop( //定义循环体 Expression.IfThenElse( //创建条件表达式 if else Expression.GreaterThanOrEqual(valueExp, Expression.Constant(1)) //valueExp>=1 , Expression.MultiplyAssign(resultExp, Expression.PostDecrementAssign(valueExp)) // if true resultExp*=valueExp-- , Expression.Break(lableExp, resultExp) // if else 跳转到lableExp,并将resultExp传入到跳转到lableExp ) , lableExp // 跳转到lableExp ) ); var lambda = Expression.Lambda <Func <int, int> >(blockExp, valueExp); int value = lambda.Compile().Invoke(6); WriteLine("{0}:{1}", lambda.Body, value); WriteLine("lambda:{0}", ParsingExpressionTrees.ParsingExpression(lambda)); WriteLine("=============================================================================="); WriteLine("ParasingExpressionTreeByVisitor:"); WriteLine("{0}", ParasingExpressionTreeByVisitor.Parsing(lambda)); WriteLine("=============================================================================="); } { Expression <Func <int, int> > lambda = num => num + 100; WriteLine("lambda :{0} ", ParsingExpressionTrees.ParsingExpression(lambda)); } }
private void Test2() { var ParsingExpressionTrees = new ParsingExpressionTreeByVisitor(); ParameterExpression pExp = Expression.Parameter(typeof(String), "p"); //创建参数表达式 p ConstantExpression aExp = Expression.Constant("A", typeof(String)); ConstantExpression bExp = Expression.Constant("B", typeof(String)); ConstantExpression cExp = Expression.Constant("C", typeof(String)); ConstantExpression dExp = Expression.Constant("D", typeof(String)); SwitchExpression switchExp = Expression.Switch(pExp , Expression.Constant("未知") , Expression.SwitchCase(Expression.Constant("aaaa", typeof(String)), aExp) , Expression.SwitchCase(Expression.Constant("bbbb", typeof(String)), bExp) , Expression.SwitchCase(Expression.Constant("cccc", typeof(String)), cExp) , Expression.SwitchCase(Expression.Constant("dddd", typeof(String)), dExp) ); LambdaExpression lambdaExp = Expression.Lambda(switchExp, pExp); String strLambda = ParsingExpressionTrees.Parsing(switchExp); WriteLine("{0}", strLambda); }
public override void Test() { { Expression< Func<String, bool>> where = p => (p.ToLower() == "coho winery" || p.Length > 16); Expression<Func<String, int>> order = p => p.Length; var Queryable = companies.Where(where.Compile()).OrderBy(order.Compile()).AsQueryable(); WriteLine("companies.Where(company => (p.ToLower() == \"coho winery\" || p.Length > 16)).OrderBy(p => p.Length).AsQueryable():"); foreach (var item in Queryable) { WriteLine(item); } WriteLine("============================ParsingExpressionTrees=========================="); WriteLine(parsing.ParsingExpression(where)); WriteLine(parsing.ParsingExpression(order)); WriteLine("======================================================"); WriteLine("============================ParsingExpressionTreeByVisitor=========================="); WriteLine(parsing2.Parsing(where)); WriteLine(parsing2.Parsing(order)); WriteLine("======================================================"); } { var queryableData = companies.AsQueryable(); var p1 = Expression.Parameter(typeof(IQueryable<String>), "queryableData"); var p = Expression.Parameter(typeof(string), "p"); var toLowerExp = Expression.Call(p, typeof(string).GetMethod("ToLower", new Type[0])); var lengthExp = Expression.Property(p, "Length"); var exp1 = Expression.Equal(toLowerExp, Expression.Constant("coho winery",typeof(string))); var exp2 = Expression.GreaterThan(lengthExp, Expression.Constant(16, typeof(int))); var predicateBody = Expression.OrElse(exp1, exp2); //创建一个表达式表示:'queryableData.Where(p => (p.ToLower() == "coho winery" || p.Length > 16)) MethodCallExpression whereCallExpression = Expression.Call( typeof(Queryable), "Where", new Type[] { queryableData.ElementType }, queryableData.Expression, Expression.Lambda<Func<string, bool>>(predicateBody, new ParameterExpression[] { p })); MethodCallExpression orderByCallExpression = Expression.Call( typeof(Queryable), "OrderBy", new Type[] { queryableData.ElementType, typeof(int) }, whereCallExpression, Expression.Lambda<Func<String,int>>(lengthExp, new ParameterExpression[] { p })); WriteLine("============================ParsingExpressionTrees=========================="); WriteLine(parsing.ParsingExpression(orderByCallExpression)); WriteLine("======================================================"); WriteLine("============================ParsingExpressionTreeByVisitor=========================="); WriteLine(parsing2.Parsing(orderByCallExpression)); WriteLine("======================================================"); var Queryable = queryableData.Provider.CreateQuery<string>(orderByCallExpression); foreach (var item in Queryable) { WriteLine(item); } } }