예제 #1
0
        public void Enumerable_GetMethod_Any()
        {
            MethodInfo anyMethod = EnumerableMethodInfo.Any <int>();

            Assert.NotNull(anyMethod);
            Assert.True(anyMethod.IsGenericMethod);
        }
예제 #2
0
        public void Enumerable_GetMethod_Select()
        {
            MethodInfo selectMethod = EnumerableMethodInfo.Select <int, int>();

            Assert.NotNull(selectMethod);
            Assert.True(selectMethod.IsGenericMethod);
        }
        public void ChangeBinaryToMethodCall()
        {
            var addresses = Expression.MakeMemberAccess(
                Expression.Parameter(typeof(Person), "person"),
                typeof(Person).GetProperty(nameof(Person.Addresses)));

            var count = Expression.MakeMemberAccess(addresses, typeof(Address).GetProperty("Count"));

            // person.PersonId == "1"
            BinaryExpression personIdEqualsOne =
                Expression.MakeBinary(
                    ExpressionType.Equal,
                    Expression.MakeMemberAccess(
                        Expression.Parameter(typeof(Person), "person"),
                        typeof(Person).GetProperty(nameof(Person.PersonId))),
                    Expression.Constant("1"));


            // person.PersonId
            Expression personId = personIdEqualsOne.Left;

            // person.Addresses
            MemberExpression personAddresses = new ChangeMemberProperty(
                typeof(Person),
                personId,
                nameof(Person.Addresses))
                                               .Change();

            // Select method
            MethodInfo selectMethod = EnumerableMethodInfo.Select <Address, int>();

            // address
            ParameterExpression addressParam = Expression.Parameter(typeof(Address), "address");

            // address.AddressId
            MemberExpression memberAccess = Expression.MakeMemberAccess(
                addressParam,
                typeof(Address).GetProperty(nameof(Address.AddressId)));

            // address => address.AddressId
            LambdaExpression lambda = Expression.Lambda(
                memberAccess,
                addressParam
                );

            // person.Addresses.Select(address => address.AddressId)
            MethodCallExpression selectAddresses = Expression.Call(selectMethod, new List <Expression> {
                personAddresses, lambda
            });

            Assert.NotNull(selectAddresses);
        }