protected override Expression VisitMember(MemberExpression node) { // Member expression could be in form of simple fields or // properties if (node.Expression is ConstantExpression) { var ci = (ConstantExpression)node.Expression; var mi = (FieldInfo)node.Member; Value = mi.GetValue(ci.Value); } else if (node.Member is PropertyInfo) { var pi = (PropertyInfo)node.Member; // Visiting subexpression recursivly to obtain // property objects value (for example, for expression () => foo.X // pi will contain "X" and value - "foo" var visitor = new CustomVisitor(); visitor.Visit(node.Expression); var value = visitor.Value; if (visitor.Processed) { Value = pi.GetValue(value); } } return(node); }
public static string Query <T>(this T obj, Expression <Func <T, bool> > filter) { CustomVisitor visitor = new CustomVisitor(); Expression epress = visitor.Visit(filter); return(visitor.ToSqlCommand()); }
protected override Expression VisitMethodCall(MethodCallExpression node) { var methodInfo = node.Method; var visitor = new CustomVisitor(); visitor.Visit(node.Object); if (visitor.Processed) { Value = methodInfo.Invoke(visitor.Value, null); } else { Value = Expression.Lambda(node).Compile().DynamicInvoke(); } return(node); }
private static object ProcessExpression(Expression expression) { var visitor = new CustomVisitor(); visitor.Visit(expression); if (visitor.Processed) { return(visitor.Value); } // Can't process this expression with our custom visitor. // Using heavy-weight compilation var argAsObj = Expression.Convert(expression, typeof(object)); return(Expression.Lambda <Func <object> >(argAsObj, null).Compile()()); }