Пример #1
0
        public static Expression CreateKeyConversionExpression(
            Expression source,
            MemberInfo[] toMembers,
            int[] mapping,
            IKeyInfoHelper from,
            IKeyInfoHelper to)
        {
            int memberCount = mapping.Length;

            Expression[] factoryArgs = new Expression[memberCount];

            for (int i = 0; i < memberCount; i++)
            {
                Type       requestedType = ReflectionHelper.GetMemberType(toMembers[i]);
                Expression arg           = from.CreateKeyMemberSelectorExpression(source, mapping[i]);

                if (arg.Type != requestedType)
                {
                    arg = Expression.Convert(arg, requestedType);
                }

                factoryArgs[i] = arg;
            }

            return(to.CreateKeyFactoryExpression(factoryArgs));
        }
Пример #2
0
        public static Expression CreateKeyEmptinessDetector(
            Expression source,
            IKeyInfoHelper helper)
        {
            Expression body        = null;
            int        memberCount = helper.GetMemberCount();

            for (int i = 0; i < memberCount; i++)
            {
                Expression member     = helper.CreateKeyMemberSelectorExpression(source, i);
                Type       memberType = member.Type;

                if (ReflectionHelper.IsNullable(memberType))
                {
                    Expression equalityTest =
                        Expression.Equal(
                            member,
                            Expression.Constant(null, memberType));

                    if (body == null)
                    {
                        body = equalityTest;
                    }
                    else
                    {
                        body = Expression.Or(body, equalityTest);
                    }
                }
            }

            // If all the members of the anonymous type is not nullable, the body expression
            // is null at this point
            if (body == null)
            {
                body = Expression.Constant(false);
            }

            return(body);
        }
Пример #3
0
        public GenericKeyComparer(SortOrder[] sortOrders, IKeyInfoHelper helper)
        {
            if (helper == null)
            {
                throw new ArgumentNullException("helper");
            }

            int memberCount = helper.GetMemberCount();

            if (memberCount <= 0)
            {
                throw new ArgumentException(
                          ExceptionMessages.InvalidKeyInfoHelper,
                          "helper");
            }

            if (sortOrders == null)
            {
                sortOrders = Enumerable.Repeat(SortOrder.Ascending, memberCount).ToArray();
            }

            if (sortOrders.Length != memberCount)
            {
                throw new ArgumentException(
                          ExceptionMessages.MemberAndSortOrderCountMismatch,
                          "sortOrders");
            }

            ParameterExpression x = Expression.Parameter(typeof(T), "x");
            ParameterExpression y = Expression.Parameter(typeof(T), "y");

            ParameterExpression variable = Expression.Variable(typeof(int), "var");

            List <Expression> blockBody = new List <Expression>();

            for (int i = 0; i < memberCount; i++)
            {
                Expression xMember = helper.CreateKeyMemberSelectorExpression(x, i);
                Expression yMember = helper.CreateKeyMemberSelectorExpression(y, i);

                Expression expr =
                    Expression.Assign(
                        variable,
                        this.CreateComparsion(xMember, yMember, sortOrders[i]));

                if (0 < i)
                {
                    expr =
                        Expression.IfThen(
                            Expression.Equal(variable, Expression.Constant(0)),
                            expr);
                }

                blockBody.Add(expr);
            }

            // Eval the last variable
            blockBody.Add(variable);

            var lambda = Expression.Lambda <Func <T, T, int> >(
                Expression.Block(
                    new ParameterExpression[] { variable },
                    blockBody),
                x,
                y);

            this.comparer = lambda.Compile();
        }