public void Action2() { var result = 0; Action <int> set = x => result = x; ExprHelper.Apply((int x, int y) => set(x + y), 1).Compile()(2); Assert.AreEqual(3, result); }
public void Action3_null() { var result = ""; Action <string> set = x => result = x; ExprHelper.Apply((string x, string y, string z) => set(x + y + z), null).Compile()("foo", "bar"); Assert.AreEqual("foobar", result); }
public void Action3() { var result = 0; Action <int> set = x => result = x; ExprHelper.Apply((int x, int y, int z) => set(x + y + z), 1).Compile()(2, 3); Assert.AreEqual(6, result); }
public void Action1() { var result = 0; Action <int> set = x => result = x; ExprHelper.Apply(x => set(x), 1).Compile()(); Assert.AreEqual(1, result); }
public IfThenExpression(Expression predicate, Expression thenExpression) { Validators.NullCheck(predicate, "predicate"); Validators.NullCheck(thenExpression, "thenExpression"); Validators.PrimitiveOrReferenceType(predicate.ExpressionType, "predicate"); _predicate = predicate; _thenExpression = ExprHelper.PopIfNeeded(thenExpression); }
/// <summary> /// Creates catch block for exception of exceptionType, where exception instance is not required. /// </summary> /// <param name="exceptionType">Exception type.</param> /// <param name="catchExpression">Catch expression.</param> public CatchBlock(Type exceptionType, Expression catchExpression) { Validators.NullCheck(exceptionType, "exceptionType"); Validators.NullCheck(catchExpression, "catchExpression"); Validators.HierarchyCheck(exceptionType, typeof(Exception), "Provided type {0} has to be deriving from {1}", "exceptionType"); ExceptionType = exceptionType; _preCatchExpression = new ExceptionCatchInitializerExpression(exceptionType, null, false); _catchExpression = ExprHelper.PopIfNeeded(catchExpression); }
public BlockExpression(Expression[] expressions) { Validators.NullCollectionElementsCheck(expressions, "expressions"); _expressions = new Expression[expressions.Length]; for (int i = 0; i < expressions.Length; i++) { _expressions[i] = ExprHelper.PopIfNeeded(expressions[i]); } }
public MethodBodyBuilder AddStatement(Expression expression) { if (expression == null) { throw new ArgumentNullException("expression"); } _statements.Add(ExprHelper.PopIfNeeded(expression)); return(this); }
internal ValueBlockExpression(Type valueType, Expression[] expressions) : base(valueType) { Validators.NullCollectionElementsCheck(expressions, "expressions"); ValidateResultType(valueType, expressions); _expressions = new Expression[expressions.Length]; for (int i = 0; i < expressions.Length; i++) { _expressions[i] = (i + 1 < expressions.Length) ? ExprHelper.PopIfNeeded(expressions[i]) : expressions[i]; } }
/// <summary> /// Creates catch block for exception of exceptionType. /// Exception instance would be available through exceptionVariable, which could be used in catchExpression. /// </summary> /// <param name="exceptionType">Exception type.</param> /// <param name="exceptionVariable">Variable used to access caught exception</param> /// <param name="declareVariable">If true, exception variable would be declared before use</param> /// <param name="catchExpression">Catch expression.</param> public CatchBlock(Type exceptionType, LocalVariable exceptionVariable, bool declareVariable, Expression catchExpression) { Validators.NullCheck(exceptionType, "exceptionType"); Validators.NullCheck(catchExpression, "catchExpression"); Validators.NullCheck(exceptionVariable, "exceptionVariable"); Validators.HierarchyCheck(exceptionType, typeof(Exception), "Provided type {0} has to be deriving from {1}", "exceptionType"); Validators.HierarchyCheck(exceptionType, exceptionVariable.VariableType, "Unable to assign exception of type {0} to local of type {1}", "exceptionVariable"); ExceptionType = exceptionType; _exceptionVariable = exceptionVariable; _preCatchExpression = new ExceptionCatchInitializerExpression(exceptionType, exceptionVariable, declareVariable); _catchExpression = ExprHelper.PopIfNeeded(catchExpression); }
public TryCatchFinallyExpression(Expression tryExpression, Expression finallyExpression, params CatchBlock[] catchBlocks) { Validators.NullCheck(tryExpression, "tryExpression"); Validators.NullCollectionElementsCheck(catchBlocks, "catchBlocks"); if (catchBlocks.Length == 0 && finallyExpression == null) { throw new ArgumentException("Try-Catch-Finally block has to have finally block or at least one catch block"); } ValidateOrderOfCatchBlocks(catchBlocks); _tryExpression = ExprHelper.PopIfNeeded(tryExpression); if (finallyExpression != null) { _finallyExpression = ExprHelper.PopIfNeeded(finallyExpression); } _catchBlocks = catchBlocks; }
public void GetConstExprValue() { Assert.Equal(-1, ExprHelper.GetConstExprValue(Expression.Constant(-1))); Assert.Equal(-2, ExprHelper.GetConstExprValue(Expression.Constant(-2))); Assert.Equal(0, ExprHelper.GetConstExprValue(Expression.Constant(0))); Assert.Equal(1, ExprHelper.GetConstExprValue(Expression.Constant(1))); Assert.Equal(2, ExprHelper.GetConstExprValue(Expression.Constant(2))); var arr = new[] { -1, -2, 0, 1, 2 }; for (var a = 0; a < arr.Length; a++) { Assert.Equal(arr[a], ExprHelper.GetConstExprValue(Expression.Constant(arr[a]))); } var arritems = new[] { new ArrItem { Prop = -1, Field = -1 }, new ArrItem { Prop = -2, Field = -2 }, new ArrItem { Prop = 0, Field = 0 }, new ArrItem { Prop = 1, Field = 1 }, new ArrItem { Prop = 2, Field = 2 }, }; for (var a = 0; a < arr.Length; a++) { Assert.Equal(arritems[a].Prop, ExprHelper.GetConstExprValue(Expression.Constant(arritems[a].Prop))); Assert.Equal(arritems[a].Field, ExprHelper.GetConstExprValue(Expression.Constant(arritems[a].Field))); } }
public void Func3_null() { var expr = ExprHelper.Apply((string x, string y, string z) => x + y + z, null).Compile(); Assert.AreEqual("foobar", expr("foo", "bar")); }
/* * //(a==1 || b == 2 || c == 3) and (d==1 || e==2) * .WhereDynamic(u => { * * var expr1 = u.NewExpr(); * * expr1.Or(x => x.a == 1); * expr1.Or(x => x.b == 2); * expr1.Or(x => x.c == 3); * * var expr2= u.NewExpr(); * * expr2.Or(x => x.d == 1); * expr2.Or(x => x.e == 2); * * var expr3 = u.NewExpr(); * expr3.And(expr1); * expr3.And(expr2); * return expr3; * }) */ /// <summary> /// 動態的Where條件式,適用於很複雜的條件組合。 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="source"></param> /// <param name="dynexpr">動態條件陳述</param> /// <returns></returns> static public IQueryable <T> WhereDynamic <T>(this IQueryable <T> source, Func <ExprHelper <T>, Expr <T> > dynexpr) { var helper = new ExprHelper <T>(); var expr = dynexpr(helper); //no expression if (expr == null || expr.Body == null) { return(source); } //資料項目參數 var dataparam = Expression.Parameter(typeof(T), "_x_"); //整個語法樹轉換為 C# Expression Expression convertToExpression(ExprNode <T> node) { switch (node.Kind) { //value case 1: return(node.Value); //or case 2: case 3: { var expr1 = convertToExpression(node.Left); var expr2 = convertToExpression(node.Right); Expression cexpr1 = expr1; //Lambda x => x.Price > 500 只取 x.Price > 500 //並將參數統一換成唯一的 if (expr1.NodeType == ExpressionType.Lambda) { var v1 = expr1 as Expression <Func <T, bool> >; var v2 = new ParameterReplacer(v1.Parameters[0], dataparam).Visit(v1) as Expression <Func <T, bool> >; cexpr1 = v2.Body; } Expression cexpr2 = expr2; //Lambda x => x.Price > 500 只取 x.Price > 500 //並將參數統一換成唯一的 if (expr2.NodeType == ExpressionType.Lambda) { var v1 = expr2 as Expression <Func <T, bool> >; var v2 = new ParameterReplacer(v1.Parameters[0], dataparam).Visit(v1) as Expression <Func <T, bool> >; cexpr2 = v2.Body; } //or if (node.Kind == 2) { return(Expression.OrElse(cexpr1, cexpr2)); } //and else { return(Expression.AndAlso(cexpr1, cexpr2)); } } } throw new NotImplementedException(); } var expr_result = convertToExpression(expr.Body); //已經是Lambda if (expr_result.NodeType == ExpressionType.Lambda) { return(source.Where(expr_result as Expression <Func <T, bool> >)); } //為And節點 | Or節點 轉換為Lambda return(source.Where(Expression.Lambda <Func <T, bool> >(expr_result, dataparam))); }
/// <summary> /// 生成该节点对应的表达式 /// </summary> public void GenExpr() { if (!this.Expr.Equals("null")) { return; } // 若为起点则表达式为1 if (this.IsStart) { this.Expr = "1"; return; } // 生成所有子项表达式 List <String> subexprs = new List <String>(); foreach (LGEdge lge in BackEdges) { LGVertex pv = lge.Source; // 若前向边指向的节点未访问(表达式为"null"),则生成表达式 if (pv.Expr.Equals("null")) { pv.GenExpr(); } string uexpr = lge.PLCInfo.Expr; // 若与运算两边的表达式其中为“1”,则忽略这个“1” if (uexpr.Equals("1")) { lge.Expr = pv.Expr; } else if (pv.Expr.Equals("1")) { lge.Expr = uexpr; } else { lge.Expr = String.Format("{0:s}&&{1:s}", pv.Expr, uexpr); } } // 如果有多个表达式需要或运算 if (BackEdges.Count > 1) { // 按照ASCII码来排序,为后续合并工作做准备 Comparison <LGEdge> sortByExpr = delegate(LGEdge v1, LGEdge v2) { return(v1.Expr.CompareTo(v2.Expr)); }; BackEdges.Sort(sortByExpr); foreach (LGEdge lge in BackEdges) { subexprs.Add(lge.Expr); } // 表达式合并 bool hasor = false; Expr = ExprHelper.Merge(subexprs, 0, subexprs.Count - 1, ref hasor); if (hasor) { Expr = "(" + Expr + ")"; } } // 如果存在前向边 else if (BackEdges.Count > 0) { this.Expr = BackEdges[0].Expr; } else { this.Expr = "1"; } }
public SelectClause SetSelect <TIn, TOut>(Expression <Func <TIn, TOut> > select) => SetSelect(ExprHelper.AddParam <TIn, object, TOut>(select));
public SelectClause SetWhere <TIn>(Expression <Func <TIn, bool> > expr) => SetWhere(ExprHelper.AddParam <TIn, object, bool>(expr));
public void Func1() { var expr = ExprHelper.Apply(x => x, 1).Compile(); Assert.AreEqual(1, expr()); }
public void Func2() { var expr = ExprHelper.Apply((int x, int y) => x + y, 1).Compile(); Assert.AreEqual(3, expr(2)); }
public LoopExpression(Expression loop) { Validators.NullCheck(loop, "loop"); _loop = ExprHelper.PopIfNeeded(loop); }
public void Func3() { var expr = ExprHelper.Apply((int x, int y, int z) => x + y + z, 1).Compile(); Assert.AreEqual(6, expr(2, 3)); }