コード例 #1
0
        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);
            }
        }
コード例 #2
0
        public void TupleKeyInfoHelper_MemberCount()
        {
            TupleKeyInfoHelper builder = new TupleKeyInfoHelper(typeof(Tuple <int, string>));

            int memberCount = builder.GetMemberCount();

            Assert.AreEqual(2, memberCount);
        }
コード例 #3
0
        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);
        }
コード例 #4
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);
        }
コード例 #5
0
        public bool TryCreateKeyInfoHelper(
            Type keyType,
            out IKeyInfoHelper result)
        {
            if (!ReflectionHelper.IsTuple(keyType))
            {
                result = null;
                return(false);
            }

            result = new TupleKeyInfoHelper(keyType);
            return(true);
        }
コード例 #6
0
        public void TupleKeyInfoHelper_KeyMemberSelectorLarge()
        {
            var tuple = new Tuple <int, int, int, int, int, int, int, Tuple <int, int> >(
                1, 2, 3, 4, 5, 6, 7, new Tuple <int, int>(8, 9));

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

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

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

            Assert.AreEqual(8, result);
        }
コード例 #7
0
        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);
        }
コード例 #8
0
        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);
        }
コード例 #9
0
        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);
        }
コード例 #10
0
ファイル: ExpressionHelper.cs プロジェクト: Promact/nmemory
        public static LambdaExpression CreateMemberSelectorLambdaExpression(
            Expression[] members,
            params ParameterExpression[] parameters)
        {
            if (members.Length == 1)
            {
                return(Expression.Lambda(members[0], parameters));
            }
            else
            {
                Type[] tupleTypes = members.Select(m => m.Type).ToArray();
                Type   tuple      = TupleTypeHelper.CreateTupleType(tupleTypes);

                var helper = new TupleKeyInfoHelper(tuple);

                return(Expression.Lambda(
                           helper.CreateKeyFactoryExpression(members),
                           parameters));
            }
        }
コード例 #11
0
        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);
        }