C# 表达式树 Expression
2021-05-17 23:29
标签:调用 base 账号 voc member 表达 public 语句 exe 表达式树是定义代码的数据结构。 它们基于编译器用于分析代码和生成已编译输出的相同结构。 几种常见的表达式 BinaryExpression 包含二元运算符的表达式 BlockExpression 包含一个表达式序列的块,表达式中可定义变量 程序执行结果 ConditionalExpression 具有条件运算符的表达式 Expression conditionExpr = Expression.Condition(Expression.Constant(num > 10),Expression.Constant("num is greater than 10"),Expression.Constant("num is smaller than 10")); ConstantExpression 具有常数值的表达式 DefaultExpression 类型或空表达式的默认值 ParameterExpression 命名的参数表达式 ParameterExpression param = Expression.Parameter(typeof(int)); IndexExpression 编制属性或数组的索引 InvocationExpression 将委托或 lambda 表达式应用于参数表达式列表的表达式 LambdaExpression 描述一个 lambda 表达式。 这将捕获与 .NET 方法体类似的代码块 ElementInit 表示 IEnumerable 集合的单个元素的初始值设定项 ListInitExpression 表示包含集合初始值设定项的构造函数调用 NewExpression 构造函数调用 LoopExpression 无限循环。 可以使用“break”退出它 LabelTarget 表示 GotoExpression 的目标 MemberAssignment 针对对象的字段或属性的赋值运算 MemberBinding 提供一种基类,该基类派生表示绑定的类,这些绑定用于初始化新创建对象的成员 MemberExpression 访问字段或属性 MemberInitExpression 调用构造函数并初始化新对象的一个或多个成员 MemberListBinding 初始化新创建对象的集合成员的元素 MemberMemberBinding 初始化新创建对象的成员的成员 NewArrayExpression 创建新数组并可能初始化该新数组的元素 SwitchCase SwitchExpression 的一个事例 SwitchExpression 一个控制表达式,该表达式通过将控制传递到 SwitchCase 来处理多重选择 TryExpression try/catch/finally/fault 块 CatchBlock try 块中的 catch 语句 UnaryExpression 包含一元运算符的表达式 UnaryExpression typeAsExpression =Expression.TypeAs(Expression.Constant(34, typeof(int)),typeof(int?));//等价 34 as int?; 微软文档地址: System.Linq.Expressions:https://docs.microsoft.com/zh-cn/dotnet/api/system.linq.expressions?view=netframework-4.8 ExpressionType:https://docs.microsoft.com/zh-cn/dotnet/api/system.linq.expressions.expressiontype?view=netframework-4.8#System_Linq_Expressions_ExpressionType_Add C# 表达式树 Expression 标签:调用 base 账号 voc member 表达 public 语句 exe 原文地址:https://www.cnblogs.com/Dewumu/p/11760686.html 1 BinaryExpression binaryExpression = Expression.MakeBinary(ExpressionType.Add,Expression.Constant(1),Expression.Constant(2));
2 Console.WriteLine(binaryExpression.ToString());//(1+2) 不进行溢出检查
3 binaryExpression = Expression.MakeBinary(ExpressionType.AddChecked, Expression.Constant(3), Expression.Constant(4));
4 Console.WriteLine(binaryExpression.ToString());//(3+4) 进行溢出检查
5 binaryExpression = Expression.MakeBinary(ExpressionType.Subtract, Expression.Constant(5), Expression.Constant(6));
6 Console.WriteLine(binaryExpression.ToString());//(5-6) 不进行溢出检查
7 binaryExpression = Expression.MakeBinary(ExpressionType.SubtractChecked, Expression.Constant(7), Expression.Constant(8));
8 Console.WriteLine(binaryExpression.ToString());//(7-8) 进行溢出检查
9 binaryExpression = Expression.MakeBinary(ExpressionType.Multiply, Expression.Constant(9), Expression.Constant(10));
10 Console.WriteLine(binaryExpression.ToString());//(9*10) 不进行溢出检查
11 binaryExpression = Expression.MakeBinary(ExpressionType.MultiplyChecked, Expression.Constant(11), Expression.Constant(12));
12 Console.WriteLine(binaryExpression.ToString());//(11*12) 进行溢出检查
13 binaryExpression = Expression.MakeBinary(ExpressionType.Divide, Expression.Constant(13), Expression.Constant(14));
14 Console.WriteLine(binaryExpression.ToString());//(13/14)
15 binaryExpression = Expression.MakeBinary(ExpressionType.Modulo, Expression.Constant(15), Expression.Constant(16));
16 Console.WriteLine(binaryExpression.ToString());//(15%16)
1 BlockExpression blockExpr = Expression.Block(
2 Expression.Call(null, typeof(Console).GetMethod("Write", new Type[] { typeof(String) }), Expression.Constant("Hello ")),
3 Expression.Call(null, typeof(Console).GetMethod("WriteLine", new Type[] { typeof(String) }), Expression.Constant("World!")),
4 Expression.Constant(42)
5 );
6 var result = Expression.Lambda
1 Expression.Constant(5.5);
2 Expression.Constant("Hello World!");
1 Expression defaultExpr = Expression.Default(typeof(byte));
2
3 // Print out the expression.
4 Console.WriteLine(defaultExpr.ToString());// 等价于 default(byte)
5
6 // The following statement first creates an expression tree,
7 // then compiles it, and then executes it.
8 Console.WriteLine(Expression.Lambda
1 ParameterExpression arrayExpr = Expression.Parameter(typeof(int[]), "Array");
2 ParameterExpression indexExpr = Expression.Parameter(typeof(int), "Index");
3 ParameterExpression valueExpr = Expression.Parameter(typeof(int), "Value");
4 Expression arrayAccessExpr = Expression.ArrayAccess(
5 arrayExpr,
6 indexExpr
7 );//Array[Index]
1 Expression
1 ParameterExpression paramExpr = Expression.Parameter(typeof(int), "arg");
2 LambdaExpression lambdaExpr = Expression.Lambda(Expression.Add(paramExpr,Expression.Constant(1)),new List
1 string tree1 = "maple";
2 string tree2 = "oak";
3
4 MethodInfo addMethod = typeof(Dictionaryint, string>).GetMethod("Add");
5
6 // Create two ElementInit objects that represent the
7 // two key-value pairs to add to the Dictionary.
8 ElementInit elementInit1 =Expression.ElementInit(addMethod,Expression.Constant(tree1.Length),Expression.Constant(tree1));
9 ElementInit elementInit2 =Expression.ElementInit(addMethod,Expression.Constant(tree2.Length),Expression.Constant(tree2));
10
11 // Create a NewExpression that represents constructing
12 // a new instance of Dictionary
1 ParameterExpression value = Expression.Parameter(typeof(int), "value");
2 ParameterExpression result = Expression.Parameter(typeof(int), "result");
3 LabelTarget label = Expression.Label(typeof(int));
4 BlockExpression block = Expression.Block(
5 new[] { result },
6 Expression.Assign(result, Expression.Constant(1)),
7 Expression.Loop(
8 Expression.IfThenElse(
9 Expression.GreaterThan(value, Expression.Constant(1)),
10 Expression.MultiplyAssign(result,
11 Expression.PostDecrementAssign(value)),
12 Expression.Break(label, result)
13 ),
14 label
15 )
16 );
17 //var s =value=>
18 //{
19 // var result = 1;
20 // for (int i = value; i >1; i--)
21 // {
22 // result *= i;
23 // }
24 // return result;
25 //};
1 public class BaseEntity
2 {
3 ///
1 BaseEntity entity = new BaseEntity();
2 NewExpression newExp = Expression.New(typeof(BaseEntity));
3
4 MemberInfo createMan = typeof(BaseEntity).GetMember("CreateMan")[0];
5 MemberInfo trMan = typeof(BaseEntity).GetMember("TrMan")[0];
6 MemberBinding createManMemberBinding = Expression.Bind(createMan, Expression.Constant("horse"));
7 MemberBinding trManMemberBinding = Expression.Bind(trMan, Expression.Constant("admin"));
8 MemberInitExpression memberInitExpression = Expression.MemberInit(newExp, createManMemberBinding, trManMemberBinding);
9
10 Console.WriteLine(memberInitExpression.ToString());
1 List
1 ConstantExpression switchValue = Expression.Constant(2);
2 SwitchExpression switchExpr =Expression.Switch(switchValue,new SwitchCase[] {
3 Expression.SwitchCase(Expression.Call(null,
4 typeof(Console).GetMethod("WriteLine", new Type[] { typeof(String) }),
5 Expression.Constant("First")),Expression.Constant(1)),
6 Expression.SwitchCase(Expression.Call(null,
7 typeof(Console).GetMethod("WriteLine", new Type[] { typeof(String) }),
8 Expression.Constant("Second")),Expression.Constant(2))});
9 Expression.Lambda(switchExpr).Compile()();
1 TryExpression tryCatchExpr =Expression.TryCatch(
2 Expression.Block(
3 Expression.Throw(Expression.Constant(new DivideByZeroException())),
4 Expression.Constant("Try block")
5 ),
6 Expression.Catch(
7 typeof(DivideByZeroException),
8 Expression.Constant("Catch block")
9 ));
文章标题:C# 表达式树 Expression
文章链接:http://soscw.com/index.php/essay/86926.html