常用的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表達式||

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM