欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#表达式目录树示例详解

程序员文章站 2024-01-01 12:30:22
1、表达式目录树  表达式目录树,在c#中是expression来定义的,它是一种语法树,或者说是一种数据结构。其主要用于存储需要计算、运算的一种结构,它只提供存储功能,不进行运算。通常express...

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

总结

到此这篇关于c#表达式目录树的文章就介绍到这了,更多相关c#表达式目录树内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

上一篇:

下一篇: