Exemplo n.º 1
0
        //public void Visit(TakeNode node)
        //{
        //    var caller = this.ContextExpression.Pop();

        //    var types = new List<Type>() { caller.Type.IsGenericType ? caller.Type.GenericTypeArguments[0] : caller.Type };
        //    var exp = Expression.Call(typeof(Queryable), "Take", types.ToArray(), caller, Expression.Constant(node.Take));
        //    this.ContextExpression.Push(exp);

        //}

        //public void Visit(SkipNode node)
        //{
        //    var caller = this.ContextExpression.Pop();

        //    var types = new List<Type>() { caller.Type.IsGenericType ? caller.Type.GenericTypeArguments[0] : caller.Type };
        //    var exp = Expression.Call(typeof(Queryable), "Skip", types.ToArray(), caller, Expression.Constant(node.Skip));
        //    this.ContextExpression.Push(exp);

        //}

        public void VisitConstant(QNode node)
        {
            Type valueType = null;

            if (node.Value.GetType().IsGenericType)
            {
                valueType = node.Value.GetType().GetGenericArguments()[0];
            }
            else
            {
                valueType = node.Value.GetType();
            }

            var memberType = ContextExpression.Peek().Type;

            if (valueType != memberType)
            {
                if (node.Value.GetType().IsGenericType)
                {
                    var list = (List <string>)node.Value;
                    if (memberType == typeof(long))
                    {
                        var exp = Expression.Constant(list.ConvertAll(Convert.ToInt64));
                        ContextExpression.Push(exp);
                    }
                    else if (memberType == typeof(DateTime))
                    {
                        var exp = Expression.Constant(list.ConvertAll(Convert.ToDateTime));
                        ContextExpression.Push(exp);
                    }
                    else
                    {
                        //var listType = typeof(List<>);
                        //var concreteType = listType.MakeGenericType(memberType);
                        //var valueList = Activator.CreateInstance(concreteType);
                        //var methodInfo = valueList.GetType().GetMethod("Add");
                        //foreach (var stringValue in (List<string>)node.Value)
                        //{
                        //    var value = ConvertConstant(stringValue, propertyMap.DestinationPropertyType, out operatorType);
                        //    methodInfo.Invoke(valueList, new object[] { value });
                        //}
                        throw new Exception(string.Format("VisitConstantNode :Type {0}", memberType));
                    }
                }
                else
                {
                    var value = Convert.ChangeType(node.Value, memberType);
                    var exp1  = Expression.Constant(value);
                    ContextExpression.Push(exp1);
                }
            }
            else
            {
                var exp = Expression.Constant(node.Value, ContextExpression.Peek().Type);
                ContextExpression.Push(exp);
            }
        }
Exemplo n.º 2
0
        public void VisitProjection(QNode node)
        {
            var left = ContextExpression.Pop();

            Type projectorType     = null;
            var  dynamicProperties = new List <DynamicProperty>();
            var  bindings1         = MemberNodeConverter.ConvertToBindings(ContextParameters.Peek(), node);

            foreach (var binding in bindings1)
            {
                var property = new DynamicProperty(binding.Key, binding.Value.Type);
                dynamicProperties.Add(property);
            }

            projectorType = ClassFactory.Instance.GetDynamicClass(dynamicProperties);

            var resultProperties   = projectorType.GetProperties();
            var bindingsSource     = new List <Expression>();
            var bindingsProperties = new List <PropertyInfo>();

            foreach (var binding in bindings1)
            {
                bindingsProperties.Add(resultProperties.FirstOrDefault(x => x.Name == binding.Key));
                bindingsSource.Add(binding.Value);
            }

            var bindings = new List <MemberBinding>();

            for (var i = 0; i < bindingsSource.Count; i++)
            {
                var exp = Expression.Bind(bindingsProperties[i], bindingsSource[i]);
                bindings.Add(exp);
            }

            var lambda =
                Expression.Lambda(
                    Expression.MemberInit(Expression.New(projectorType.GetConstructor(Type.EmptyTypes)), bindings),
                    ContextParameters.Peek());

            var result = BuildMethodCallExpression(MethodType.Select, left, lambda);

            ContextExpression.Push(result);

            HasProjection        = true;
            Mapper.EnableMapping = false;
        }
Exemplo n.º 3
0
        public void VisitBinary(QNode node)
        {
            var        right = ContextExpression.Pop();
            var        left  = ContextExpression.Pop();
            BinaryType op;

            if (node.Value is long)
            {
                op = (BinaryType)Convert.ToInt16(node.Value);
            }
            else
            {
                Enum.TryParse(Convert.ToString(node.Value), out op);
            }
            var exp = BuildBinaryExpression(op, left, right);

            ContextExpression.Push(exp);
        }
Exemplo n.º 4
0
        public void VisitMethod(QNode node)
        {
            MethodType method;

            if (node.Value is long)
            {
                method = (MethodType)Convert.ToInt16(node.Value);
            }
            else
            {
                Enum.TryParse(Convert.ToString(node.Value), out method);
            }

            var right = ContextExpression.Pop();
            var left  = ContextExpression.Pop();

            var lambda = Expression.Lambda(right, ContextParameters.Peek());

            var exp = BuildMethodCallExpression(method, left, lambda);

            ContextExpression.Push(exp);
        }
Exemplo n.º 5
0
 public void VisitQuerable(QNode node)
 {
     ContextExpression.Push(Query);
     Mapper.EnableMapping = true;
 }
Exemplo n.º 6
0
        public void VisitMember(QNode node)
        {
            var member = MemberNodeConverter.ConvertToMemberExpression(ContextParameters.Peek(), node);

            ContextExpression.Push(member);
        }