public void TupleKeyInfoHelper_ParseLarge()
        {
            TupleKeyInfoHelper builder = new TupleKeyInfoHelper(typeof(Tuple<int, string, int, string, int, string, int, Tuple<string, int>>));

            Expression<Func<Group, Tuple<int, string, int, string, int, string, int, Tuple<string, int>>>> 
                selector = (Group m) =>
                    new Tuple<int, string, int, string, int, string, int, Tuple<string, int>>(
                        m.Id, 
                        m.Name, 
                        m.Id, 
                        m.Name, 
                        m.Id, 
                        m.Name,
                        m.Id, 
                        new Tuple<string, int>(
                            m.Name,
                            m.Id));
                        
            MemberInfo[] members = null;
            bool res = builder.TryParseKeySelectorExpression(selector.Body, true, out members);

            Assert.AreEqual(true, res);
            Assert.IsNotNull(members);
            Assert.AreEqual(9, members.Length);

            for (int i = 0; i < 9; i++)
            {
                string expect = i % 2 == 0 ? "Id" : "Name";
                Assert.AreEqual(expect, members[i].Name);
            }
        }
        public void TupleKeyInfoHelper_ParseCreate()
        {
            TupleKeyInfoHelper builder = new TupleKeyInfoHelper(typeof(Tuple<int, string>));

            Expression<Func<Group, Tuple<int, string>>> selector =
                (Group m) => Tuple.Create(m.Id, m.Name);

            MemberInfo[] members = null;
            bool res = builder.TryParseKeySelectorExpression(selector.Body, true, out members);

            Assert.AreEqual(true, res);
            Assert.IsNotNull(members);
            Assert.AreEqual(2, members.Length);
            Assert.AreEqual("Id", members[0].Name);
            Assert.AreEqual("Name", members[1].Name);
        }
예제 #3
0
        private static Expression CreateTupleSelector(
            Expression body, 
            Expression[] memberSelectors)
        {
            Type[] memberTypes = memberSelectors.Select(e => e.Type).ToArray();

            Type tupleType = TupleTypeHelper.CreateTupleType(memberTypes);

            var helper = new TupleKeyInfoHelper(tupleType);
            body = helper.CreateKeyFactoryExpression(memberSelectors);
            return body;
        }
        public void TupleKeyInfoHelper_KeyMemberSelectorLarge2()
        {
            var tuple = 
                new Tuple<int, int, int, int, int, int, int, 
                    Tuple<int, int, int, int, int, int, int, 
                        Tuple<int, int>>>(
                1, 2, 3, 4, 5, 6, 7, 
                    new Tuple<int, int, int, int, int, int, int, Tuple<int, int>>(
                        8, 9, 10, 11, 12, 13, 14, Tuple.Create(15, 16)));

            TupleKeyInfoHelper builder = new TupleKeyInfoHelper(tuple.GetType());

            Expression source = Expression.Constant(tuple);
            Expression selector = builder.CreateKeyMemberSelectorExpression(source, 15);

            int result = (int)Expression.Lambda(selector).Compile().DynamicInvoke();

            Assert.AreEqual(16, result);
        }
        public void TupleKeyInfoHelper_KeyMemberSelector()
        {
            TupleKeyInfoHelper builder = new TupleKeyInfoHelper(typeof(Tuple<int, string>));

            Expression source = Expression.Constant(Tuple.Create(1, "2"));
            Expression selector1 = builder.CreateKeyMemberSelectorExpression(source, 0);
            Expression selector2 = builder.CreateKeyMemberSelectorExpression(source, 1);

            int result1 = (int)Expression.Lambda(selector1).Compile().DynamicInvoke();
            string result2 = (string)Expression.Lambda(selector2).Compile().DynamicInvoke();

            Assert.AreEqual(1, result1);
            Assert.AreEqual("2", result2);
        }
        public void TupleKeyInfoHelper_KeyFactoryLarge()
        {
            TupleKeyInfoHelper builder =
                new TupleKeyInfoHelper(typeof(Tuple<int, int, int, int, int, int, int, Tuple<int, int>>));

            Expression factory = builder.CreateKeyFactoryExpression(
                Enumerable
                    .Range(1, 9)
                    .Select(x => Expression.Constant(x, typeof(int)))
                    .ToArray());

            var result = Expression.Lambda(factory).Compile().DynamicInvoke()
                as Tuple<int, int, int, int, int, int, int, Tuple<int, int>>;

            Assert.IsNotNull(result);

            Assert.AreEqual(1, result.Item1);
            Assert.AreEqual(2, result.Item2);
            Assert.AreEqual(8, result.Rest.Item1);
            Assert.AreEqual(9, result.Rest.Item2);
        }
        public void TupleKeyInfoHelper_KeyFactory()
        {
            TupleKeyInfoHelper builder = new TupleKeyInfoHelper(typeof(Tuple<int, string>));

            Expression factory = builder.CreateKeyFactoryExpression(
                Expression.Constant(1, typeof(int)), 
                Expression.Constant("2", typeof(string)));

            var result = Expression.Lambda(factory).Compile().DynamicInvoke() as Tuple<int, string>;

            Assert.IsNotNull(result);
            Assert.AreEqual(1, result.Item1);
            Assert.AreEqual("2", result.Item2);
        }
        public void TupleKeyInfoHelper_MemberCountLarge()
        {
            TupleKeyInfoHelper builder =
                new TupleKeyInfoHelper(typeof(Tuple<int, int, int, int, int, int, int, Tuple<int, int>>));

            int memberCount = builder.GetMemberCount();

            Assert.AreEqual(9, memberCount);
        }
        public void TupleKeyInfoHelper_MemberCount()
        {
            TupleKeyInfoHelper builder = new TupleKeyInfoHelper(typeof(Tuple<int, string>));

            int memberCount = builder.GetMemberCount();

            Assert.AreEqual(2, memberCount);
        }