public object Execute(Expression expression) { object source = null; if (expression.NodeType == ExpressionType.Call) { expression = new SourceReplacement().Visit(expression); Debug.Assert(expression.NodeType == ExpressionType.Call); var method = ((MethodCallExpression)expression).Method; //Debug.Assert(method.IsGenericMethod); var args = ((MethodCallExpression)expression).Arguments.Select(o => ExpressionCalculater.Eval(o, null)).ToArray(); source = method.Invoke(null, args); } else { source = ExpressionCalculater.Eval(expression); if (source is IQueryable) { var type = source.GetType(); Debug.Assert(type.IsGenericType); Debug.Assert(type.GetGenericArguments().Length == 1); var t = typeof(List <>).MakeGenericType(source.GetType().GetGenericArguments()[0]); var list = (IList)Activator.CreateInstance(t); foreach (var item in (IQueryable)source) { list.Add(item); } source = list; } } return(source); }
private void VisitNegate(UnaryExpression node) { var value = ExpressionCalculater.Eval(node.Operand, null); value = Arithmetic.Negate(value, node.Type); PushValue(value); }
static Func <T, TResult> CreateFunc1 <T, TResult>(LambdaExpression lamb) { Debug.Assert(lamb.Parameters.Count == 1); Func <T, TResult> f = delegate(T arg) { var p = new ObjectParameter(lamb.Parameters[0].Name, arg); var result = ExpressionCalculater.Eval(lamb.Body, p); return((TResult)result); }; return(f); }
static Func <T1, T2, TResult> CreateFunc2 <T1, T2, TResult>(LambdaExpression lamb) { Debug.Assert(lamb.Parameters.Count == 2); Func <T1, T2, TResult> f = delegate(T1 arg0, T2 arg1) { var p0 = new ObjectParameter(lamb.Parameters[0].Name, arg0); var p1 = new ObjectParameter(lamb.Parameters[1].Name, arg1); var result = ExpressionCalculater.Eval(lamb.Body, p0, p1); return((TResult)result); }; return(f); }
protected override Expression VisitMemberAccess(MemberExpression m) { if (typeof(IQueryable).IsAssignableFrom(m.Type)) { Debug.Assert(m.Expression.NodeType == ExpressionType.Constant); this.queryProvider = (IQueryProvider)ExpressionCalculater.Eval(m); } if (this.queryProvider != null) { return(m); } return(base.VisitMemberAccess(m)); }