Esempio n. 1
0
 public System.Linq.Expressions.Expression GetConstructorExpression()
 {
     System.Linq.Expressions.ConstantExpression value = System.Linq.Expressions.Expression.Constant(this.Value, typeof(double));
     System.Linq.Expressions.ConstantExpression units = System.Linq.Expressions.Expression.Constant(this.Units, typeof(PageUnits));
     System.Linq.Expressions.NewExpression      ctor  = System.Linq.Expressions.Expression.New(ctorRef, value, units);
     return(ctor);
 }
        protected override System.Linq.Expressions.Expression VisitConstant(System.Linq.Expressions.ConstantExpression node)
        {
            var table = TableInfoManager.GetTable(node.Type.GetGenericArguments()[0]);

            ExtraObject = table.Name;
            return(base.VisitConstant(node));
        }
 protected override System.Linq.Expressions.Expression VisitConstant(System.Linq.Expressions.ConstantExpression c)
 {
     if (c.Value != null)
     {
         if (c.Value.GetType() == typeof(DMSDataBase))
         {
             _foundDMSDataBase = true;
             string text = ((DMSDataBase)c.Value).DataBase;
             if (this.SplitExpression.TableMapping.TokenFlag == true)
             {
                 text = this.Provider.BuildColumnName(text);
             }
             this._strSql.Append(text);
             this._strSql.Append(this.Provider.TableToken);
         }
         else if (c.Value.ToString() == this.Provider.InnerJoin ||
                  c.Value.ToString() == this.Provider.RightJoin ||
                  c.Value.ToString() == this.Provider.LeftJoin ||
                  c.Value.ToString() == this.Provider.On ||
                  c.Value.ToString() == this.Provider.As)
         {
             this._strSql.Append(c.Value.ToString());
         }
     }
     return(base.VisitConstant(c));
 }
            public Delegate MakeDelegate(Type deltype)
            {
#if NETFX_CORE
                Delegate rv = null;
                if (_Cache.TryGetValue(deltype, out rv))
                {
                    return(rv);
                }

                System.Linq.Expressions.ParameterExpression[] expars = new System.Linq.Expressions.ParameterExpression[0];
                var pars = deltype.GetMethod("Invoke").GetParameters();
                if (pars != null)
                {
                    expars = new System.Linq.Expressions.ParameterExpression[pars.Length];
                    for (int i = 0; i < pars.Length; ++i)
                    {
                        expars[i] = System.Linq.Expressions.Expression.Parameter(pars[i].ParameterType, pars[i].Name);
                    }
                }
                System.Linq.Expressions.ConstantExpression extar = System.Linq.Expressions.Expression.Constant(this);
                rv = System.Linq.Expressions.Expression.Lambda(
                    deltype,
                    System.Linq.Expressions.Expression.Call(extar, "Call", null, expars),
                    true,
                    expars
                    ).Compile();
                _Cache[deltype] = rv;
                return(rv);
#else
                return(Delegate.CreateDelegate(deltype, this, "Call"));
#endif
            }
Esempio n. 5
0
        protected override System.Linq.Expressions.Expression VisitConstant(System.Linq.Expressions.ConstantExpression c)
        {
            StringBuilder stringBuilder = new StringBuilder();

            if (c.Value != null)
            {
                if (c.Type.IsArray)
                {
                    Array array = c.Value as Array;
                    foreach (object current in array)
                    {
                        if (current.GetType().IsClass)
                        {
                            System.Linq.Expressions.ConstantExpression consExpr = System.Linq.Expressions.Expression.Constant(current);
                            return(this.VisitConstant(consExpr));
                        }
                        else
                        {
                            if (current != null)
                            {
                                this.AdjustConstant(current, ref stringBuilder);
                            }
                            else
                            {
                                stringBuilder.Append("NULL");
                            }
                            stringBuilder.Append(",");
                        }
                    }
                    if (this.ExcuteType == DMSExcuteType.SELECT ||
                        this.ExcuteType == DMSExcuteType.UPDATE ||
                        this.ExcuteType == DMSExcuteType.UPDATE_WHERE)
                    {
                        this._strSql.Append(stringBuilder.ToString().Trim(new char[] { ',' }));
                    }
                }
                else
                {
                    this.AdjustConstant(c.Value, ref stringBuilder);
                    if (this.ExcuteType == DMSExcuteType.SELECT ||
                        this.ExcuteType == DMSExcuteType.UPDATE ||
                        this.ExcuteType == DMSExcuteType.UPDATE_WHERE)
                    {
                        this._strSql.Append(stringBuilder.ToString());
                    }
                    else if (this.ExcuteType == DMSExcuteType.INSERT ||
                             this.ExcuteType == DMSExcuteType.INSERTIDENTITY ||
                             this.ExcuteType == DMSExcuteType.INSERT_SELECT)
                    {
                        this._ParamSql.Append(stringBuilder.ToString());
                    }
                }
            }
            else
            {
                this._strSql.Append(" NULL ");
            }
            return(base.VisitConstant(c));
        }
        /// <summary>
        /// 表达式路由
        /// </summary>
        /// <param name="exp"></param>
        /// <returns></returns>
        string ExpressionRouter(System.Linq.Expressions.Expression exp)
        {
            //获取实体列的特性
            List <EntityPropColumnAttributes> columnAttrList = AttributeHelper.GetEntityColumnAtrributes <TEntity>();

            string sb = string.Empty;

            if (exp is System.Linq.Expressions.BinaryExpression)//二元运算符
            {
                System.Linq.Expressions.BinaryExpression be = ((System.Linq.Expressions.BinaryExpression)exp);
                return(BinarExpressionProvider(be.Left, be.Right, be.NodeType));
            }
            else if (exp is System.Linq.Expressions.MemberExpression)//成员
            {
                System.Linq.Expressions.MemberExpression me = ((System.Linq.Expressions.MemberExpression)exp);
                return(me.Member.Name);
            }
            else if (exp is System.Linq.Expressions.NewArrayExpression)//数组
            {
                System.Linq.Expressions.NewArrayExpression ae = ((System.Linq.Expressions.NewArrayExpression)exp);
                StringBuilder tmpstr = new StringBuilder();
                foreach (System.Linq.Expressions.Expression ex in ae.Expressions)
                {
                    tmpstr.Append(ExpressionRouter(ex));
                    tmpstr.Append(",");
                }
                return(tmpstr.ToString(0, tmpstr.Length - 1));
            }
            else if (exp is System.Linq.Expressions.MethodCallExpression)//方法
            {
                return(MethodExpression(exp));
            }
            else if (exp is System.Linq.Expressions.ConstantExpression)
            {
                System.Linq.Expressions.ConstantExpression ce = ((System.Linq.Expressions.ConstantExpression)exp);
                if (ce.Value == null)
                {
                    return("null");
                }
                else if (ce.Value is ValueType)
                {
                    return(ce.Value.ToString());
                }
                else if (ce.Value is string || ce.Value is DateTime || ce.Value is char)
                {
                    return(string.Format("{0}", ce.Value.ToString()));
                }
            }
            else if (exp is System.Linq.Expressions.UnaryExpression)
            {
                System.Linq.Expressions.UnaryExpression ue = ((System.Linq.Expressions.UnaryExpression)exp);
                return(ExpressionRouter(ue.Operand));
            }
            return(null);
        }
Esempio n. 7
0
        protected override System.Linq.Expressions.Expression VisitConstant(System.Linq.Expressions.ConstantExpression c)
        {
            StringBuilder stringBuilder = new StringBuilder();

            if (c.Value == null)
            {
                this._strSql.Append(" NULL ");
                return(base.VisitConstant(c));
            }
            if (c.Type.IsArray)
            {
                Array array = c.Value as Array;
                foreach (object current in array)
                {
                    if (current != null)
                    {
                        this.AdjustConstant(current, ref stringBuilder);
                    }
                    else
                    {
                        stringBuilder.Append("NULL");
                    }
                    stringBuilder.Append(",");
                }
                this._strSql.Append(stringBuilder.ToString().Trim(new char[] { ',' }));
            }
            else if (c.Type.GetInterface(typeof(IList).FullName, false) != null)
            {
                IList list = c.Value as IList;
                foreach (var current in list)
                {
                    if (current != null)
                    {
                        this.AdjustConstant(current, ref stringBuilder);
                    }
                    else
                    {
                        stringBuilder.Append("NULL");
                    }
                    stringBuilder.Append(",");
                }
                this._strSql.Append(stringBuilder.ToString().Trim(new char[] { ',' }));
            }
            else
            {
                this.AdjustConstant(c.Value, ref stringBuilder);
                this._strSql.Append(stringBuilder.ToString());
            }

            return(base.VisitConstant(c));
        }
Esempio n. 8
0
        //public static System.Action<T, object> GetSetter<T>(string fieldName)
        public static Setter_t <T> GetSetter <T>(string fieldName)
        {
            // Class in which to set value
            System.Linq.Expressions.ParameterExpression targetExp = System.Linq.Expressions.Expression.Parameter(typeof(T), "target");

            // Object's type:
            System.Linq.Expressions.ParameterExpression valueExp = System.Linq.Expressions.Expression.Parameter(typeof(object), "value");


            // Expression.Property can be used here as well
            System.Linq.Expressions.MemberExpression memberExp = null;
            try
            {
                // memberExp = System.Linq.Expressions.Expression.Field(targetExp, fieldName);
                // memberExp = System.Linq.Expressions.Expression.Property(targetExp, fieldName);
                memberExp = System.Linq.Expressions.Expression.PropertyOrField(targetExp, fieldName);
            }
            catch (System.Exception ex)
            {
                return(null);
            }


            // http://www.dotnet-tricks.com/Tutorial/linq/RJX7120714-Understanding-Expression-and-Expression-Trees.html
            System.Linq.Expressions.ConstantExpression targetType = System.Linq.Expressions.Expression.Constant(memberExp.Type);

            // http://stackoverflow.com/questions/913325/how-do-i-make-a-linq-expression-to-call-a-method
            System.Linq.Expressions.MethodCallExpression mce = System.Linq.Expressions.Expression.Call(m_FlexibleChangeType, valueExp, targetType);


            //System.Linq.Expressions.UnaryExpression conversionExp = System.Linq.Expressions.Expression.Convert(valueExp, memberExp.Type);
            System.Linq.Expressions.UnaryExpression conversionExp = System.Linq.Expressions.Expression.Convert(mce, memberExp.Type);


            System.Linq.Expressions.BinaryExpression assignExp =
                // System.Linq.Expressions.Expression.Assign(memberExp, valueExp); // Without conversion
                System.Linq.Expressions.Expression.Assign(memberExp, conversionExp);

            // System.Action<TTarget, TValue> setter = System.Linq.Expressions.Expression
            // System.Action<T, object> setter = System.Linq.Expressions.Expression
            // .Lambda<System.Action<T, object>>(assignExp, targetExp, valueExp).Compile();

            Setter_t <T> setter = System.Linq.Expressions.Expression
                                  .Lambda <Setter_t <T> >(assignExp, targetExp, valueExp).Compile();

            return(setter);
        } // End Function GetGetter
Esempio n. 9
0
        /// <summary>Visits a constant expression.</summary>
        /// <param name="expression">Expression to be visited.</param>
        /// <returns>Expression visited</returns>
        protected override System.Linq.Expressions.Expression VisitConstant(System.Linq.Expressions.ConstantExpression expression)
        {
            if ((expression.Value is IEnumerable) && (!(expression.Value is string)))
            {
                List        list  = new List();
                IEnumerable value = (IEnumerable)expression.Value;
                foreach (object item in value)
                {
                    if (item is IEntity)
                    {
                        list.Values.Add(new Literal(((IEntity)item).Id.Uri));
                    }
                    else if (item is EntityId)
                    {
                        list.Values.Add(new Literal(((EntityId)item).Uri));
                    }
                    else
                    {
                        list.Values.Add(new Literal(ResolveRelativeUriIfNecessery(item)));
                    }
                }

                _lastComponent = list;
            }
            else if (expression.Value is IEntity)
            {
                HandleComponent(_lastComponent = new Literal(((IEntity)expression.Value).Id.Uri));
            }
            else if (expression.Value != null)
            {
                HandleComponent(_lastComponent = new Literal(ResolveRelativeUriIfNecessery(expression.Value)));
            }
            else
            {
                Type valueType = FindLiteralType();
                HandleComponent(_lastComponent = new Literal(valueType));
            }

            return(expression);
        }
Esempio n. 10
0
        /// <summary>
        /// 通过FilterCondition解析得到Lambda表达式
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="filter"></param>
        /// <returns></returns>
        public static System.Linq.Expressions.Expression <Func <T, bool> > GetLambdaByFilter <T>(FilterCondition filter)
        {
            System.Linq.Expressions.ParameterExpression mParameter = System.Linq.Expressions.Expression.Parameter(typeof(T), filter.FieldName);
            System.Linq.Expressions.MemberExpression    member     = System.Linq.Expressions.Expression.Property(mParameter, filter.FieldName);
            Type type = typeof(T).GetProperty(filter.FieldName).PropertyType;

            //构造单个参数值和多个参数值的常量表达式
            System.Linq.Expressions.ConstantExpression constant = System.Linq.Expressions.Expression.Constant(Convert.ChangeType(filter.Value, type), type);

            List <System.Linq.Expressions.ConstantExpression> lstConstant = null;

            if (filter.ListValue != null && filter.ListValue.Count() > 0)
            {
                lstConstant = new List <System.Linq.Expressions.ConstantExpression>();
                foreach (var item in filter.ListValue)
                {
                    lstConstant.Add(System.Linq.Expressions.Expression.Constant(Convert.ChangeType(item, type), type));
                }
            }
            System.Linq.Expressions.Expression operation = null;
            switch (filter.Operator)
            {
            case FilterOperator.Equals:
                operation = System.Linq.Expressions.Expression.Equal(member, constant);
                break;

            case FilterOperator.DoesNotEqual:
                operation = System.Linq.Expressions.Expression.NotEqual(member, constant);
                break;

            case FilterOperator.IsGreaterThan:
                operation = System.Linq.Expressions.Expression.GreaterThan(member, constant);
                break;

            case FilterOperator.IsGreaterThanOrEqaulTo:
                operation = System.Linq.Expressions.Expression.GreaterThanOrEqual(member, constant);
                break;

            case FilterOperator.IsLessThan:
                operation = System.Linq.Expressions.Expression.LessThan(member, constant);
                break;

            case FilterOperator.IsLessThanOrEqaulTo:
                operation = System.Linq.Expressions.Expression.LessThanOrEqual(member, constant);
                break;

            case FilterOperator.Contains:
                System.Reflection.MethodInfo containsMethod = typeof(string).GetMethod("Contains");
                operation = System.Linq.Expressions.Expression.Call(member, containsMethod, constant);
                break;

            case FilterOperator.Or:
                foreach (var item in lstConstant)
                {
                    System.Linq.Expressions.Expression expression = System.Linq.Expressions.Expression.Equal(member, item);
                    if (operation == null)
                    {
                        operation = expression;
                    }
                    else
                    {
                        operation = System.Linq.Expressions.Expression.Or(operation, expression);
                    }
                }
                break;

            default:
                break;
            }
            return(System.Linq.Expressions.Expression.Lambda <Func <T, bool> >(operation, mParameter));
        }
Esempio n. 11
0
 /// <summary>
 /// Converts a LINQ constant expression into the Infer.NET equivalent.
 /// </summary>
 /// <param name="constantExpression"></param>
 /// <returns></returns>
 private IExpression ConvertConstant(System.Linq.Expressions.ConstantExpression constantExpression)
 {
     return(Builder.LiteralExpr(constantExpression.Value));
 }
 protected override void PVisitConstant(System.Linq.Expressions.ConstantExpression c)
 {
     Push(walkerFactory.FromConstantNode(c));
     base.PVisitConstant(c);
 }