Exemple #1
0
        public static object GetValue(this MemberBinding binding)
        {
            switch (binding.BindingType)
            {
            case MemberBindingType.Assignment:
                var assign = (MemberAssignment)binding;
                if (assign.Expression is ConstantExpression constant)
                {
                    return(constant.Value);
                }

                try
                {
                    return(CachedExpressionCompiler.Evaluate(assign.Expression));
                }
                catch (Exception ex)
                {
                    //Log.Error("Error compiling expression in MemberBinding.GetValue()", ex);

                    //Fallback to compile and execute
                    var member = Expression.Convert(assign.Expression, typeof(object));
                    var lambda = Expression.Lambda <Func <object> >(member);
                    var getter = lambda.Compile();
                    return(getter());
                }
            }
            return(null);
        }
Exemple #2
0
        public static string[] GetFieldNames <T>(this Expression <Func <T, object> > expr)
        {
            if (expr.Body is MemberExpression member)
            {
                if (member.Member.DeclaringType.IsAssignableFrom(typeof(T)))
                {
                    return new[] { member.Member.Name }
                }
                ;

                var array = CachedExpressionCompiler.Evaluate(member);
                if (array is IEnumerable <string> strEnum)
                {
                    return(strEnum.ToArray());
                }
            }

            if (expr.Body is NewExpression newExpr)
            {
                return(newExpr.Arguments.OfType <MemberExpression>().Select(x => x.Member.Name).ToArray());
            }

            if (expr.Body is MemberInitExpression init)
            {
                return(init.Bindings.Select(x => x.Member.Name).ToArray());
            }

            if (expr.Body is NewArrayExpression newArray)
            {
                var constantExprs = newArray.Expressions.OfType <ConstantExpression>().ToList();
                if (newArray.Expressions.Count == constantExprs.Count)
                {
                    return(constantExprs.Select(x => x.Value.ToString()).ToArray());
                }

                var array = CachedExpressionCompiler.Evaluate(newArray);
                if (array is string[] strArray)
                {
                    return(strArray);
                }

                return(((IEnumerable)array).Cast <object>()
                       .Select(x => x.ToString())
                       .ToArray());
            }

            if (expr.Body is UnaryExpression unary)
            {
                member = unary.Operand as MemberExpression;
                if (member != null)
                {
                    return new[] { member.Member.Name }
                }
                ;
            }

            throw new ArgumentException("Invalid Fields List Expression: " + expr);
        }
        private object ToInPartialString(Expression memberExpr, string memberName)
        {
            var result = CachedExpressionCompiler.Evaluate(memberExpr);

            var items    = Flatten(result as IEnumerable);
            var dbParams = items.Map(GetValueAsParam);
            var expr     = $"{memberName} IN ({string.Join(",", dbParams)})";

            return(new PartialString(expr));
        }
        protected virtual object VisitMethodCall(MethodCallExpression m)
        {
            if (m.Method.DeclaringType == typeof(Dynamo))
            {
                return(VisitDynamoMethodCall(m));
            }

            if (IsStaticArrayMethod(m))
            {
                return(VisitStaticArrayMethodCall(m));
            }

            if (IsEnumerableMethod(m))
            {
                return(VisitEnumerableMethodCall(m));
            }

            if (IsColumnAccess(m))
            {
                return(VisitColumnAccessMethod(m));
            }

            return(CachedExpressionCompiler.Evaluate(m));
        }