C#-表達式樹

目錄

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

1、表達式目錄樹

  表達式目錄樹,在C#中是Expression來定義的,它是一種語法樹,或者說是一種數據結構。其主要用於存儲需要計算、運算的一種結構,它只提供存儲功能,不進行運算。通常Expression是配合Lambda一起使用,lambda可以是匿名方法。Expression可以動態創建。

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

//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來定義:

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

  Expression的方法體只能是一個整體,不能具有花括弧,以下程式碼是不允許的:

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);

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。

  如何構建一個複雜的表達式目錄樹?需要使用到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”);

  這個示例中,使用到了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
});

3、使用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");

4、表達式目錄樹構建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); //比較值

 5、修改表達式目錄樹

  本示例將把已經構建完成的表達式目錄樹的加法進行修改為減法。修改、拼接、讀取節點,需要使用到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的解析方法。

6、構建模擬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());
}

7、連接表達式目錄樹

  表達式目錄樹除了可以修改外,我們還可以通過對其進行表達式目錄樹的拼接,將兩個及其以上的表達式目錄樹進行拼接在一起。先編寫一個新的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條件!

 

Tags: