日期:2014-05-20 浏览次数:20798 次
public class MyData : YouData { } public class YouData { public int v1 { get; set; } public int v2 { get; set; } } public class Service { public int count(Expression<Func<YouData, bool>> exp) { //便于测试 return 1; } } //重写Expression 访问类 public class MyExpressionVisitor : System.Linq.Expressions.ExpressionVisitor { public ParameterExpression Parameter { get; set; } public Expression ModifyExp(Expression exp) { return this.Visit(exp); } protected override Expression VisitParameter(ParameterExpression node) { return Parameter; } } public class MyApp { public int count(Expression<Func<MyData, bool>> exp) { ParameterExpression param = Expression.Parameter(typeof(YouData), "name"); MyExpressionVisitor expVisitor = new MyExpressionVisitor(); expVisitor.Parameter = param; Expression expBody = expVisitor.ModifyExp(exp.Body); var finalExp = Expression.Lambda<Func<YouData, bool>>(expBody, param); return new Service().count(finalExp); } }