常用的Expression调用形式

ConstantExpression exp1 = Expression.Constant(1);构建常量表达式(还可以加类型)
BinaryExpression exp12 = Expression.Add(exp1, exp2);构建二元加法表达式,参数为左右两个需要相加的常量表达式或者变量
ParameterExpression expA = Expression.Parameter(typeof(double), "a");构建参数
-------------------------------------------------------------
ParameterExpression expA = Expression.Parameter(typeof(double), "a"); //参数a
MethodCallExpression expCall = Expression.Call(null, 
    typeof(Math).GetMethod("Sin", BindingFlags.Static | BindingFlags.Public), 
    expA); //Math.Sin(a)构建方法调用表达式(表示一次方法调用)
---------------------------------------------------
LambdaExpression exp = Expression.Lambda(expCall, expA); //构建lambda表达式(由表达式体和参数构成这个表达式体表示为一个方法调用表达式)
---------------------------------------------------

Expression<Func<double, double>> exp = a => Math.Sin(a);构建强类型的表达式树(表达式树字面量)
----------------------------------------------------------
UnaryExpression negate= Expression.Negate(left);//构建一元-表达式如:-a
-----------------------------------------------------------------------
ConstantExpression strin=   Expression.Constant("hello",typeof(string));
NewExpression negate = Expression.New(typeof(StringBuilder).GetConstructor(new Type[] { typeof(String) }), strin);
//构造new StringBuilder("hello")
-----------------------------------------------------------------------
ParameterExpression a = Expression.Parameter(typeof(int), "a");
            ParameterExpression b= Expression.Parameter(typeof(int), "b");
            ParameterExpression i= Expression.Parameter(typeof(int), "i");
            BinaryExpression add = Expression.Add(a,b);
            ConstantExpression s=Expression.Constant(1);

            BinaryExpression substract = Expression.Subtract(i,s);

            NewArrayExpression arrayint= Expression.NewArrayInit(typeof(int), a, b, add);
            IndexExpression arracc= Expression.ArrayAccess(arrayint, substract);
            Console.WriteLine(arracc.ToString());
//new [] {a, b, (a + b)}[(i - 1)]数组访问
--------------------------------------------------------------------------
MemberExpression mem=Expression.Property(exppro, property)//创建类型的属性表达式
--------------------------------------
Expression.Equal(left, right)//二元=表达式
----------------------------------
Expression.GreaterThan(left, right)//二元>表达式
----------------------------------
Expression.GreaterThanOrEqual(left, right)//>=
-------------------------------
Expression.LessThan(left, right)//<
---------------------------------
Expression.LessThanOrEqual(left,right)/<=
--------------------------------------
return Expression.Call(left, typeof (string).GetMethod("Contains"), right)//like 字符串中包含right表达式代表的字符串
----------------------------------------------
 MethodCallExpression resultExp =
                                    Expression.Call(
                                        typeof (Enumerable),
                                        "Contains",
                                        new[] {left.Type},
                                        right,
                                        left)
//构造in 比如: c=>new[] {"nn","mm"}.contains(c.name)
--------------------------------------------
Expression.NotEqual(left, right)//!=
-------------------------------------------
Expression.Call(left, typeof (string).GetMethod("StartsWith", new[] {typeof (string)}), right)//left以right字符串开始
------------------------------------
Expression.Call(left, typeof (string).GetMethod("EndsWith", new[] {typeof (string)}), right)//left以right字符串结尾
---------------------------------------
Expression.AndAlso(left, expression)//and 表达式&&
-----------------------------------------
Expression.OrElse(left, orGroupByExpAnd)//or表达式||

 

posted on 2016-05-30 10:52  ~紫鱼~  阅读(3310)  评论(0编辑  收藏  举报