private static Expression MakeLocal(Expression e) { if (e.NodeType == ExpressionType.Constant) { return(e); } else if (e.NodeType == ExpressionType.Convert || e.NodeType == ExpressionType.ConvertChecked) { UnaryExpression ue = (UnaryExpression)e; if (ue.Type == typeof(object)) { Expression local = MakeLocal(ue.Operand); return((e.NodeType == ExpressionType.Convert) ? Expression.Convert(local, e.Type) : Expression.ConvertChecked(local, e.Type)); } // convert a const null if (ue.Operand.NodeType == ExpressionType.Constant) { ConstantExpression c = (ConstantExpression)ue.Operand; if (c.Value == null) { return(Expression.Constant(null, ue.Type)); } } } var useExpressionFunctions = Mindbox.Data.Linq.Configuration.UseExpressionFunctions(); var function = useExpressionFunctions ? ExpressionFunctionFactory.Create(e) : Expression.Lambda(e).Compile(); return(Expression.Invoke(Expression.Constant(function))); }
private static object EvaluateViaExpressionFunction(Expression expression) { var evaluator = ExpressionFunctionFactory.Create(expression); return(evaluator.DynamicInvoke()); }