C#-表達式樹

 目錄

  1. 表達式樹
  2. 構建表達式樹
  3. 使用Expression來進行不一樣對象的相同名字的屬性映射
  4. 表達式樹構建SQL刪選
  5. 修改表達式樹
  6. 構建模擬EF的表達式樹解析
  7. 鏈接表達式樹

一、表達式樹

  表達式樹,在C#中是Expression來定義的,它是一種語法樹,或者說是一種數據結構。其主要用於存儲須要計算、運算的一種結構,它只提供存儲功能,不進行運算。一般Expression是配合Lambda一塊兒使用,lambda能夠是匿名方法。Expression能夠動態建立。node

  聲明一個lambda表達式,其中能夠指明類型,也能夠是匿名方法:sql

//Func<int, int, int> func = new Func<int, int, int>((m, n) => m * n + 2);
Func<int, int, int> func = (m, n) => m * n + 2;

  上述代碼可使用Expression來定義:express

Expression<Func<int, int, int>> exp = (m, n) => m * n + 2;//lambda表達式聲明表達式樹

  Expression的方法體只能是一個總體,不能具備花括號,如下代碼是不容許的:json

Expression<Func<int, int, int>> exp1 = (m, n) =>//方法體只能一體
{
    return m * n + 2;
};

  上述func和exp執行結果相同:緩存

int iResult1 = func.Invoke(3, 2);
int iResult2 = exp.Compile().Invoke(3, 2);

二、構建表達式樹

  上述表達式示例能夠經過Expression來自主構建,把m、n定義爲ParameterExpression參數,把2定義爲常數表達式ConstantExpression,使用Expression的靜態方法,表示乘和加:數據結構

ParameterExpression parameterLeft = Expression.Parameter(typeof(int), "m");//定義參數
ParameterExpression parameterRight = Expression.Parameter(typeof(int), "n");//定義參數
BinaryExpression binaryMultiply = Expression.Multiply(parameterLeft, parameterRight);//組建第一步的乘法             
ConstantExpression constant = Expression.Constant(2, typeof(int)); //定義常數參數
BinaryExpression binaryAdd = Expression.Add(binaryMultiply, constant);//組建第二步的加法
var expression = Expression.Lambda<Func<int, int, int>>(binaryAdd, parameterLeft, parameterRight);//構建表達式     
var func = expression.Compile();  //編譯爲lambda表達式
int iResult3 = func(3, 2);
int iResult4 = expression.Compile().Invoke(3, 2);
int iResult5 = expression.Compile()(3, 2);

  自主構建Expression是,參數名稱的定義,能夠不是m、n,能夠是其餘的a、b或者x、y。app

  如何構建一個複雜的表達式樹?須要使用到Expression中更多的方法、屬性、擴展方法等。首先定義一個類:框架

public class People
{
    public int Age { get; set; }
    public string Name { get; set; }
    public int Id;
}

  基於上面的類,構建表達式: Expression<Func<People, bool>> lambda = x => x.Id.ToString().Equals("5");ide

  這個示例中,使用到了int自身的ToString()方法,還使用到了字符串的Equals方法。構建過程以下:測試

//如下表達式樹實現lambda的表達式
Expression<Func<People, bool>> lambda = x => x.Id.ToString().Equals("5");
//聲明一個參數對象
ParameterExpression parameterExpression = Expression.Parameter(typeof(People), "x");
//查找字段, 並綁定訪問參數對象字段(屬性)的方法:x.Id
MemberExpression member = Expression.Field(parameterExpression, typeof(People).GetField("Id"));
//以上能夠用這個代替
var temp =Expression.PropertyOrField(parameterExpression, "Id");
//調用字段的ToString方法:x.Id.ToString()
MethodCallExpression method = Expression.Call(member, typeof(int).GetMethod("ToString", new Type[] { }), new Expression[0]);
//調用字符串的Equals方法:x.Id.ToString().Equals("5")
MethodCallExpression methodEquals = Expression.Call(method, typeof(string).GetMethod("Equals", new Type[] { typeof(string) }), new Expression[]
{
    Expression.Constant("5", typeof(string))//與常量進行比較,也能夠是參數
});
//建立表達式樹
ar expression = Expression.Lambda<Func<People, bool>>(methodEquals, new ParameterExpression[] {parameterExpression });
bool bResult = expression.Compile().Invoke(new People()
{
    Id = 5,
    Name = "Nigle",
    Age = 31
});

三、使用Expression來進行不一樣對象的相同名字的屬性映射

  前面構建了類People,如今咱們構建一個新的類PeopleCopy:

public class PeopleCopy
{
    public int Age { get; set; }
    public string Name { get; set; }
    public int Id;
}

  如今聲明一個People對象,而後對People對象的數據進行拷貝到PeopleCopy新對象中去,直接硬編碼的方式:

  1. 硬編碼

People people = new People()
{
    Id = 11,
    Name = "Nigle",
    Age = 31
};
PeopleCopy peopleCopy = new PeopleCopy()
{
    Id = people.Id,
    Name = people.Name,
    Age = people.Age
};

  若是這樣編寫,對於屬性或者字段比較多的類,在拷貝時,咱們須要編寫不少次賦值,代碼也會很長。此時,咱們能想到的是經過反射的方式進行拷貝:

  2. 反射拷貝

public static TOut Trans<TIn, TOut>(TIn tIn)
{
    TOut tOut = Activator.CreateInstance<TOut>();
    foreach (var itemOut in tOut.GetType().GetProperties())
    {
        foreach (var itemIn in tIn.GetType().GetProperties())
        {
            if (itemOut.Name.Equals(itemIn.Name))
            {
                itemOut.SetValue(tOut, itemIn.GetValue(tIn));
                break;
            }
        }
    }
    foreach (var itemOut in tOut.GetType().GetFields())
    {
        foreach (var itemIn in tIn.GetType().GetFields())
        {
            if (itemOut.Name.Equals(itemIn.Name))
            {
                itemOut.SetValue(tOut, itemIn.GetValue(tIn));
                break;
            }
        }
    }
    return tOut;
}

  經過反射,咱們能夠經過輸出類型的屬性或者字段去查找原類型對應的屬性和字段,而後獲取值,並設置值的方式進行賦值拷貝。除此以外,咱們還能想到的是深克隆的序列化方式,進行反序列化數據:

  3. 序列化和反序列化

public class SerializeMapper
{
    /// <summary>序列化反序列化方式/summary>
    public static TOut Trans<TIn, TOut>(TIn tIn)
    {
        //採用的是json序列化,也能夠採用其餘序列化方式
        return JsonConvert.DeserializeObject<TOut>(JsonConvert.SerializeObject(tIn));
    }
}

  前面的三種方法是最爲經常使用的方法,但未使用到本文介紹的表達式樹。如何將表達式樹與拷貝結合起來?有兩種方式【緩存+表達式】,【泛型+表達式】

  4. 緩存+表達式樹

/// <summary>
/// 生成表達式樹 緩存
/// </summary>
public class ExpressionMapper
{
    private static Dictionary<string, object> _Dic = new Dictionary<string, object>();

    /// <summary>
    /// 字典緩存表達式樹
    /// </summary>
    public static TOut Trans<TIn, TOut>(TIn tIn)
    {
        string key = string.Format("funckey_{0}_{1}", typeof(TIn).FullName, typeof(TOut).FullName);
        if (!_Dic.ContainsKey(key))
        {
            ParameterExpression parameterExpression = Expression.Parameter(typeof(TIn), "p");
            List<MemberBinding> memberBindingList = new List<MemberBinding>();
            foreach (var item in typeof(TOut).GetProperties())
            {
                MemberExpression property = Expression.Property(parameterExpression, typeof(TIn).GetProperty(item.Name));
                //綁定Out和In之間的關係:Age = p.Age
                MemberBinding memberBinding = Expression.Bind(item, property);
                memberBindingList.Add(memberBinding);
            }
            foreach (var item in typeof(TOut).GetFields())
            {
                MemberExpression property = Expression.Field(parameterExpression, typeof(TIn).GetField(item.Name));
                MemberBinding memberBinding = Expression.Bind(item, property);
                memberBindingList.Add(memberBinding);
            }
            MemberInitExpression memberInitExpression = Expression.MemberInit(Expression.New(typeof(TOut)), memberBindingList.ToArray());
            Expression<Func<TIn, TOut>> lambda = Expression.Lambda<Func<TIn, TOut>>(memberInitExpression, parameterExpression);
            Func<TIn, TOut> func = lambda.Compile();//拼裝是一次性的
            _Dic[key] = func;
        }
        return ((Func<TIn, TOut>)_Dic[key]).Invoke(tIn);
    }
}

  5. 泛型+表達式

/// <summary>
/// 生成表達式樹  泛型緩存
/// </summary>
/// <typeparam name="TIn"></typeparam>
/// <typeparam name="TOut"></typeparam>
public class ExpressionGenericMapper<TIn, TOut>//Mapper`2
{
    private static Func<TIn, TOut> func = null;
    static ExpressionGenericMapper()
    {
        ParameterExpression parameterExpression = Expression.Parameter(typeof(TIn), "p");
        List<MemberBinding> memberBindingList = new List<MemberBinding>();
        foreach (var item in typeof(TOut).GetProperties())
        {
            MemberExpression property = Expression.Property(parameterExpression, typeof(TIn).GetProperty(item.Name));
            MemberBinding memberBinding = Expression.Bind(item, property);
            memberBindingList.Add(memberBinding);
        }
        foreach (var item in typeof(TOut).GetFields())
        {
            MemberExpression property = Expression.Field(parameterExpression, typeof(TIn).GetField(item.Name));
            MemberBinding memberBinding = Expression.Bind(item, property);
            memberBindingList.Add(memberBinding);
        }
        MemberInitExpression memberInitExpression = Expression.MemberInit(Expression.New(typeof(TOut)), memberBindingList.ToArray());
        Expression<Func<TIn, TOut>> lambda = Expression.Lambda<Func<TIn, TOut>>(memberInitExpression, new ParameterExpression[]
        {
                parameterExpression
        });
        func = lambda.Compile();//拼裝是一次性的
    }
    public static TOut Trans(TIn t)
    {
        return func(t);
    }
}

  除了上述5中方法,還可使用框架自帶的AutoMapper,首先咱們要nuget添加引用AutoMapper便可直接使用,具體代碼爲:

  6. AutoMapper

public class AutoMapperTest
{
    public static TOut Trans<TIn, TOut>(TIn tIn)
    {
        return AutoMapper.Mapper.Instance.Map<TOut>(tIn);
    }
}

  測評:對上述6種方式進行測評,每一種拷貝方式運行100 0000次:

Stopwatch watch = new Stopwatch();
watch.Start();
for (int i = 0; i < 1000000; i++)
{
    //測試六種方法
    PeopleCopy peopleCopy = new PeopleCopy()  {Id = people.Id, Name = people.Name,Age = people.Age};    //直接賦值的方式複製--22ms
    //PeopleCopy peopleCopy = ReflectionMapper.Trans<People, PeopleCopy>(people);                       //反射賦值的方式複製---1573ms
    //PeopleCopy peopleCopy = SerializeMapper.Trans<People, PeopleCopy>(people);                        //序列化方式---2716ms
    //PeopleCopy peopleCopy = ExpressionMapper.Trans<People, PeopleCopy>(people);                       //表達式樹 緩存  複製---517ms
    //PeopleCopy peopleCopy = ExpressionGenericMapper<People, PeopleCopy>.Trans(people);                //表達式樹  泛型緩存--77ms
    //PeopleCopy peopleCopy = AutoMapperTest.Trans<People, PeopleCopy>(people);                         //AutoMapper---260ms
}
watch.Stop();
Console.WriteLine($"耗時:{ watch.ElapsedMilliseconds} ms");

四、表達式樹構建SQL刪選

  傳統的sql在構建條件語句時,須要經過諸多判斷,進而構建成完整的查詢語句。

People p = new People()
{
    Id = 11, Name = "Nigle", Age = 31 }; //拼裝sql的方式 string sql = "SELECT * FROM USER WHERE Id=1"; if (string.IsNullOrWhiteSpace(p.Name)) { sql += $" and name like '%{p.Name}%'"; } sql += $" and age >{p.Age}";

  事實上,咱們偶爾咱們會使用linq查詢或者lambda表達式,用於條件篩選,如var lambda = x => x.Age > 5; 事實上,咱們能夠構建上述Expression:

People p = new People()
{
    Id = 11, Name = "Nigle", Age = 31 }; //拼裝表達式樹,交給下端用 ParameterExpression parameterExpression = Expression.Parameter(typeof(People), "x");//聲明一個參數 Expression propertyExpression = Expression.Property(parameterExpression, typeof(People).GetProperty("Age"));//聲明訪問參數屬性的對象 //Expression property = Expression.Field(parameterExpression, typeof(People).GetField("Id")); ConstantExpression constantExpression = Expression.Constant(5, typeof(int));//聲明一個常量 BinaryExpression binary = Expression.GreaterThan(propertyExpression, constantExpression);//添加比較方法 var lambda = Expression.Lambda<Func<People, bool>>(binary, new ParameterExpression[] { parameterExpression });//構建表達式主體 bool bResult = lambda.Compile().Invoke(p); //比較值

 五、修改表達式樹

  本示例將把已經構建完成的表達式樹的加法進行修改成減法。修改、拼接、讀取節點,須要使用到ExpressionVisitor類,ExpressionVisitor類能動態的解耦,讀取相關的節點和方法。

  ExpressionVisitor類中的Visit(Expression node)是解讀表達式的入口,而後可以神奇的區分參數和方法體,而後將表達式調度到此類中更專用的訪問方法中,而後一層一層的解析下去,直到最終的葉節點!

  首先編寫OperationsVisitor類,用於修改:

internal class OperationsVisitor : ExpressionVisitor
{
    public Expression Modify(Expression expression)
    {
        return this.Visit(expression);
    }
    protected override Expression VisitBinary(BinaryExpression b)
    {
        if (b.NodeType == ExpressionType.Add)
        {
            Expression left = this.Visit(b.Left);
            Expression right = this.Visit(b.Right);
            return Expression.Subtract(left, right);
        }

        return base.VisitBinary(b);
    }
    protected override Expression VisitConstant(ConstantExpression node)
    {
        return base.VisitConstant(node);
    }
}

  而後,編寫lambda表達式,進行修改並計算結果:

//修改表達式樹
Expression<Func<int, int, int>> exp = (m, n) => m * n + 2;
OperationsVisitor visitor = new OperationsVisitor();
Expression expNew = visitor.Modify(exp);

int? iResult = (expNew as Expression<Func<int, int, int>>)?.Compile().Invoke(2, 3);

  Visit這個這個方法可以識別出來 m*n+2 是個二叉樹,會經過下面的圖而後一步一步的進行解析,若是遇到m*n 這會直接調用VisitBinary(BinaryExpression b)這個方法,若是遇到m或者n會調用VisitParameter(ParameterExpression node)這個方法,若是遇到2常量則會調用VisitConstant(ConstantExpression node)。

  ORM與表達式樹的關係:

  常常用到EF,其實都是繼承Queryable,而後咱們使用的EF一般都會使用 var items = anserDo.GetAll().Where(x => x.OrganizationId == input.oid || input.oid == 0) ,where其實傳的就是表達式樹。EF寫的where等lambda表達式,就是經過ExpressionVisitor這個類來反解析的!後面將構建模擬EF的解析方法。

六、構建模擬EF的表達式樹解析

  首先,構建解析表達式樹的方法,不能再使用默認的。

/// <summary>
/// 表達式樹中的訪問者
/// </summary>
internal class ConditionBuilderVisitor : ExpressionVisitor
{
    /// <summary>
    /// 用於存放條件等數據
    /// </summary>
    private Stack<string> _StringStack = new Stack<string>();        
    /// <summary>
    /// 
    /// </summary>
    /// <returns></returns>
    internal string Condition()
    {
        string condition = string.Concat(this._StringStack.ToArray());
        this._StringStack.Clear();
        return condition;
    }
    /// <summary>
    /// 若是是二元表達式
    /// </summary>
    /// <param name="node"></param>
    /// <returns></returns>
    protected override Expression VisitBinary(BinaryExpression node)
    {
        if (node == null) throw new ArgumentNullException("BinaryExpression");

        this._StringStack.Push(")");
        base.Visit(node.Right);//解析右邊
        this._StringStack.Push(" " + ToSqlOperator(node.NodeType) + " ");
        base.Visit(node.Left);//解析左邊
        this._StringStack.Push("(");

        return node;
    }
    /// <summary>
    /// 
    /// </summary>
    /// <param name="node"></param>
    /// <returns></returns>
    protected override Expression VisitMember(MemberExpression node)
    {
        if (node == null)
            throw new ArgumentNullException("MemberExpression");
        this._StringStack.Push(" [" + node.Member.Name + "] ");
        return node;
        return base.VisitMember(node);
    }
    /// <summary>
    /// 將節點類型轉換爲Sql的操做符
    /// </summary>
    /// <param name="type"></param>
    /// <returns></returns>
    string ToSqlOperator(ExpressionType type)
    {
        switch (type)
        {
            case (ExpressionType.AndAlso):
            case (ExpressionType.And):
                return "AND";
            case (ExpressionType.OrElse):
            case (ExpressionType.Or):
                return "OR";
            case (ExpressionType.Not):
                return "NOT";
            case (ExpressionType.NotEqual):
                return "<>";
            case ExpressionType.GreaterThan:
                return ">";
            case ExpressionType.GreaterThanOrEqual:
                return ">=";
            case ExpressionType.LessThan:
                return "<";
            case ExpressionType.LessThanOrEqual:
                return "<=";
            case (ExpressionType.Equal):
                return "=";
            default:
                throw new Exception("不支持該方法");
        }
    }
    /// <summary>
    /// 常量表達式
    /// </summary>
    /// <param name="node"></param>
    /// <returns></returns>
    protected override Expression VisitConstant(ConstantExpression node)
    {
        if (node == null)
            throw new ArgumentNullException("ConstantExpression");
        this._StringStack.Push(" '" + node.Value + "' ");
        return node;
    }
    /// <summary>
    /// 方法表達式
    /// </summary>
    /// <param name="m"></param>
    /// <returns></returns>
    protected override Expression VisitMethodCall(MethodCallExpression m)
    {
        if (m == null) throw new ArgumentNullException("MethodCallExpression");

        string format;
        switch (m.Method.Name)
        {
            case "StartsWith":
                format = "({0} LIKE {1}+'%')";
                break;

            case "Contains":
                format = "({0} LIKE '%'+{1}+'%')";
                break;

            case "EndsWith":
                format = "({0} LIKE '%'+{1})";
                break;

            default:
                throw new NotSupportedException(m.NodeType + " is not supported!");
        }
        this.Visit(m.Object);
        this.Visit(m.Arguments[0]);
        string right = this._StringStack.Pop();
        string left = this._StringStack.Pop();
        this._StringStack.Push(String.Format(format, left, right));
        return m;
    }
}

  而後,外部就能夠經過編寫表達式樹的查詢條件,再經過這個類的實例進行解析成對應的SQL語句:

{
    Expression<Func<People, bool>> lambda = x => x.Age > 5 && x.Id > 5
                                             && x.Name.StartsWith("1")
                                             && x.Name.EndsWith("1")
                                             && x.Name.Contains("2");
    //「 x => x.Age > 5 && x.Id > 5」等同於sql語句
    string sql = string.Format("Delete From [{0}] WHERE {1}", typeof(People).Name, " [Age]>5 AND [ID] >5");
    ConditionBuilderVisitor vistor = new ConditionBuilderVisitor();
    vistor.Visit(lambda);
    Console.WriteLine(vistor.Condition());
}
{
    Expression<Func<People, bool>> lambda = x => x.Age > 5 && x.Name == "A" || x.Id > 5;
    ConditionBuilderVisitor vistor = new ConditionBuilderVisitor();
    vistor.Visit(lambda);
    Console.WriteLine(vistor.Condition());
}
{
    Expression<Func<People, bool>> lambda = x => x.Age > 5 || (x.Name == "A" && x.Id > 5);
    ConditionBuilderVisitor vistor = new ConditionBuilderVisitor();
    vistor.Visit(lambda);
    Console.WriteLine(vistor.Condition());
}
{
    Expression<Func<People, bool>> lambda = x => (x.Age > 5 || x.Name == "A") && x.Id > 5;
    ConditionBuilderVisitor vistor = new ConditionBuilderVisitor();
    vistor.Visit(lambda);
    Console.WriteLine(vistor.Condition());
}

七、鏈接表達式樹

  表達式樹除了能夠修改外,咱們還能夠經過對其進行表達式樹的拼接,將兩個及其以上的表達式樹進行拼接在一塊兒。先編寫一個新的NewExpressionVisitor,繼承自ExpressionVisitor,用於拼接時,調用的。它是一個內部類,放在訪問拼接類的內部ExpressionExtend。而後再編寫對應的擴展方法:Add、Or、Not

/// <summary>
/// 合併表達式 And Or  Not擴展
/// </summary>
public static class ExpressionExtend
{
    /// <summary>合併表達式 expLeft and expRight</summary>
    public static Expression<Func<T, bool>> And<T>(this Expression<Func<T,bool>> expLeft,Expression<Func<T,bool>> expRight)
    {
        //用於將參數名進行替換,兩者參數不同
        ParameterExpression newParameter = Expression.Parameter(typeof(T), "c");
        NewExpressionVisitor visitor = new NewExpressionVisitor(newParameter);
        //須要先將參數替換爲一致的,可能參數名不同
        var left = visitor.Replace(expLeft.Body);//左側的表達式
        var right = visitor.Replace(expRight.Body);//右側的表達式
        var body = Expression.And(left, right);//合併表達式
        return Expression.Lambda<Func<T, bool>>(body, newParameter);
    }
    /// <summary>合併表達式 expr1 or expr2</summary>
    public static Expression<Func<T, bool>> Or<T>(this Expression<Func<T, bool>> expr1, Expression<Func<T, bool>> expr2)
    {

        ParameterExpression newParameter = Expression.Parameter(typeof(T), "c");
        NewExpressionVisitor visitor = new NewExpressionVisitor(newParameter);
        //須要先將參數替換爲一致的,可能參數名不同
        var left = visitor.Replace(expr1.Body);
        var right = visitor.Replace(expr2.Body);
        var body = Expression.Or(left, right);
        return Expression.Lambda<Func<T, bool>>(body, newParameter);
    }
    public static Expression<Func<T, bool>> Not<T>(this Expression<Func<T, bool>> expr)
    {
        var candidateExpr = expr.Parameters[0];
        var body = Expression.Not(expr.Body);
        return Expression.Lambda<Func<T, bool>>(body, candidateExpr);
    }
    /// <summary>參數替換者 </summary>
    class NewExpressionVisitor : ExpressionVisitor
    {
        public ParameterExpression _NewParameter { get; private set; }
        public NewExpressionVisitor(ParameterExpression param)
        {
            this._NewParameter = param;//用於把參數替換了
        }
        /// <summary> 替換</summary>
        public Expression Replace(Expression exp)
        {
            return this.Visit(exp);
        }
        protected override Expression VisitParameter(ParameterExpression node)
        {
            //返回新的參數名
            return this._NewParameter;
        }
    }
}

  下面是測試代碼:

Expression<Func<People, bool>> lambda1 = x => x.Age > 5;
Expression<Func<People, bool>> lambda2 = p => p.Id > 5;
Expression<Func<People, bool>> lambda3 = lambda1.And(lambda2);
Expression<Func<People, bool>> lambda4 = lambda1.Or(lambda2);
Expression<Func<People, bool>> lambda5 = lambda1.Not();

List<People> people = new List<People>()
{
    new People(){Id=4,Name="123",Age=4},
    new People(){Id=5,Name="234",Age=5},
    new People(){Id=6,Name="345",Age=6},
};

List<People> lst1 = people.Where(lambda3.Compile()).ToList();
List<People> lst2 = people.Where(lambda4.Compile()).ToList();
List<People> lst3 = people.Where(lambda5.Compile()).ToList();

  Expression目前只支持ExpressionType的84種操做符Add, AndAlso等等,而後VisitMethodCall這個方法中表示lambda能解析出來的方法名字,若是須要能夠自行修改會獲得對應的sql語句的where條件!

相關文章
相關標籤/搜索