// // Summary: // Visits the children of the System.Linq.Expressions.BlockExpression. // // Parameters: // node: // The expression to visit. // // Returns: // The modified expression, if it or any subexpression was modified; otherwise, // returns the original expression. protected override Expression VisitBlock(BlockExpression node) { Console.WriteLine("VisitBlock:"); Console.WriteLine('\t' + node.GetType().ToString()); Console.WriteLine('\t' + node.ToString()); return(base.VisitBlock(node)); }
public void TestCompileHandle() { var args = new object[] { "a", "b" }; var instance = Expression.Constant(this); var arrayExpr = Expression.Parameter(typeof(object[]), "input"); var vars = new List <ParameterExpression>(); var assignments = new List <Expression>(); var accessExpressions = new List <Expression>(); for (var i = 0; i < args.Length; i++) { var type = args[i].GetType(); var varExpression = Expression.Variable(type, "var" + i.ToString()); vars.Add(varExpression); //ParameterExpression arrayExpr = Expression.Parameter(typeof(int[]), "Array"); // This parameter expression represents an array index. //ParameterExpression indexExpr = System.Linq.Expressions.Expression.Constant(0); ;// Expression.Parameter(typeof(int), "Index"); // This parameter represents the value that will be added to a corresponding array element. ParameterExpression valueExpr = Expression.Parameter(typeof(int), "Value"); // This expression represents an array access operation. // It can be used for assigning to, or reading from, an array element. Expression arrayAccessExpr = Expression.ArrayAccess( arrayExpr, System.Linq.Expressions.Expression.Constant(i) ); accessExpressions.Add(Expression.Convert(Expression.ArrayAccess( arrayExpr, System.Linq.Expressions.Expression.Constant(i) ), type)); Expression convert = Expression.Convert(arrayAccessExpr, type); BinaryExpression assignment = Expression.Assign(varExpression, convert); assignments.Add(assignment); } var callArgs = new List <ParameterExpression>(); var method = this.GetType().GetMethod(nameof(HandlerTest1)); var callExpression = Expression.Call(instance, method, accessExpressions); var box = Expression.Convert(callExpression, typeof(object)); assignments.Add(box); BlockExpression execute = Expression.Block(vars.Concat(assignments)); var exStr = execute.ToString(); // vars.Insert(0, arrayExpr); var fun = Expression.Lambda <Func <object[], object> >(box, arrayExpr).Compile(); var result = (string)fun(args); Assert.True(result == "ab"); }
public static void ToStringTest() { BlockExpression e1 = Expression.Block(Expression.Empty()); Assert.Equal("{ ... }", e1.ToString()); BlockExpression e2 = Expression.Block(new[] { Expression.Parameter(typeof(int), "x") }, Expression.Empty()); Assert.Equal("{var x; ... }", e2.ToString()); BlockExpression e3 = Expression.Block(new[] { Expression.Parameter(typeof(int), "x"), Expression.Parameter(typeof(int), "y") }, Expression.Empty()); Assert.Equal("{var x;var y; ... }", e3.ToString()); }
public static void Expression1() { var t1 = CustomAttributeExtensions.GetCustomAttribute(typeof(ProxyAttribute), typeof(ProxyAttribute)); // Add the following directive to your file: // using System.Linq.Expressions; // The block expression allows for executing several expressions sequentually. // When the block expression is executed, // it returns the value of the last expression in the sequence. BlockExpression blockExpr = Expression.Block( Expression.Call( null, typeof(Console).GetMethod("Write", new Type[] { typeof(String) }), Expression.Constant("Hello ") ), Expression.Call( null, typeof(Console).GetMethod("WriteLine", new Type[] { typeof(String) }), Expression.Constant("World!") ), Expression.Constant(42) ); var mehoth = Expression.Call( null, typeof(Console).GetMethod("WriteLine", new Type[] { typeof(String) }), Expression.Constant("World!") ); Console.WriteLine("The result of executing the expression tree:"); // The following statement first creates an expression tree, // then compiles it, and then executes it. var func = Expression.Lambda <Func <int> >(blockExpr).Compile(); var result = func(); var ss = blockExpr.ToString(); // Print out the expressions from the block expression. Console.WriteLine("The expressions from the block expression:"); foreach (var expr in blockExpr.Expressions) { Console.WriteLine(expr.ToString()); } // Print out the result of the tree execution. Console.WriteLine("The return value of the block expression:"); Console.WriteLine(result); }
/// <summary>访问子内容为: <see cref="T:System.Linq.Expressions.BlockExpression"></see>.</summary> /// <param name="node">被访问的表达式</param> /// <returns>The modified expression, if it or any subexpression was modified; otherwise, returns the original expression.</returns> protected override Expression VisitBlock(BlockExpression node) { Log(node.ToString()); throw new NotImplementedException(); }