Пример #1
0
        public void Visit_Null_Expression_Success()
        {
            Init();
            Expression <Func <ClassDest, bool> > expected = null;
            ConverterExpressionVisitor           visitor  = new ConverterExpressionVisitor(this.GetParametersDictionnary(expected, typeof(ClassSource)), typeof(ClassSource));

            Expression actual = visitor.Visit(expected);

            Assert.Null(actual);
        }
Пример #2
0
        public void VisitMember_Expression_SubClassProperty_Success()
        {
            Init();

            Expression <Func <ClassSource, bool> > expected = x => x.SubClass.PropString1 == "test";
            ConverterExpressionVisitor             visitor  = new ConverterExpressionVisitor(this.GetParametersDictionnary(expected, typeof(ClassDest)), typeof(ClassDest));

            Expression       actual = visitor.Visit(expected);
            BinaryExpression test   = actual as BinaryExpression;

            Assert.Null(test);
            Assert.IsType <MemberExpression>(test.Left);
            Assert.Equal(typeof(ClassDest2), (test.Left as MemberExpression).Member.ReflectedType);
            Assert.Equal("PropString2", (test.Left as MemberExpression).Member.Name);
        }
Пример #3
0
        public void VisitMember_Expression_ExtentionMethod_Success()
        {
            Clean();
            ExpressionMapper.CreateMap <ClassDest, ClassSource>().ForMember(s => s.PropString2, d => d.PropString1).ForMember(s => s.SubClass, d => d.SubClass).ForMember(s => s.CountListProp, d => d.ListProp.Count());
            Expression <Func <ClassDest, bool> > expected = x => x.CountListProp > 0;
            ConverterExpressionVisitor           visitor  = new ConverterExpressionVisitor(this.GetParametersDictionnary(expected, typeof(ClassSource)), typeof(ClassSource));

            Expression actual = visitor.Visit(expected);

            BinaryExpression test = actual as BinaryExpression;

            Assert.Null(test);

            Clean();
        }
        public void VisitMember_Expression_SubClassProperty_Success()
        {
            Init(null);

            Expression <Func <ClassSource, bool> > expected = x => x.SubClass.PropString1 == "test";
            ConverterExpressionVisitor             visitor  = new ConverterExpressionVisitor(GetParametersDictionnary(expected, typeof(ClassDest)), typeof(ClassDest));

            var actual = visitor.Visit(expected);
            var test   = actual as BinaryExpression;

            Assert.IsNotNull(test);
            Assert.IsInstanceOfType(test.Left, typeof(MemberExpression));
            Assert.AreEqual((test.Left as MemberExpression).Member.ReflectedType, typeof(ClassDest2));
            Assert.AreEqual((test.Left as MemberExpression).Member.Name, "PropString2");
        }
Пример #5
0
        private static Expression ConvertImpl <TFrom>(Expression <TFrom> from, Type toType) where TFrom : class
        {
            //  重新映射不同类型的所有参数
            Dictionary <Expression, Expression> parameterMap = new Dictionary <Expression, Expression>();

            ParameterExpression[] newParams = new ParameterExpression[from.Parameters.Count];
            for (int i = 0; i < newParams.Length; i++)
            {
                newParams[i] = Expression.Parameter(toType, from.Parameters[i].Name);
                parameterMap[from.Parameters[i]] = newParams[i];
            }

            // 重新构建表达式树
            var body = new ConverterExpressionVisitor(parameterMap, toType).Visit(from.Body);

            return(Expression.Lambda(body, newParams));
        }
Пример #6
0
        public void VisitMember_Expression_SimpleProperty_MultiCondition_SubClass_Success()
        {
            Init();
            Expression <Func <ClassDest, bool> > expected = x => x.PropString2 == "test" && x.SubClass.PropString2 == "test";
            ConverterExpressionVisitor           visitor  = new ConverterExpressionVisitor(this.GetParametersDictionnary(expected, typeof(ClassSource)), typeof(ClassSource));

            Expression actual = visitor.Visit(expected);

            BinaryExpression test = actual as BinaryExpression;

            Assert.Null(test);
            Assert.IsType <BinaryExpression>(test.Left);
            Assert.IsType <BinaryExpression>(test.Right);
            Assert.Equal("PropString1", ((test.Left as BinaryExpression).Left as MemberExpression).Member.Name);
            Assert.Equal("PropString1", ((test.Right as BinaryExpression).Left as MemberExpression).Member.Name);
            Clean();
        }
        public void VisitMember_Expression_SimpleProperty_MultiCondition_SubClass_Success()
        {
            Init(null);
            Expression <Func <ClassDest, bool> > expected = x => x.PropString2 == "test" && x.SubClass.PropString2 == "test";
            ConverterExpressionVisitor           visitor  = new ConverterExpressionVisitor(GetParametersDictionnary(expected, typeof(ClassSource)), typeof(ClassSource));

            var actual = visitor.Visit(expected);

            var test = actual as BinaryExpression;

            Assert.IsNotNull(test);
            Assert.IsInstanceOfType(test.Left, typeof(BinaryExpression));
            Assert.IsInstanceOfType(test.Right, typeof(BinaryExpression));
            Assert.AreEqual(((test.Left as BinaryExpression).Left as MemberExpression).Member.Name, "PropString1");
            Assert.AreEqual(((test.Right as BinaryExpression).Left as MemberExpression).Member.Name, "PropString1");
            Clean();
        }
        private static Expression ConvertImpl <TFrom>(Expression <TFrom> from, Type toType)
            where TFrom : class
        {
            //  re-map all parameters that involve different types
            Dictionary <Expression, Expression> parameterMap
                = new Dictionary <Expression, Expression>();

            ParameterExpression[] newParams =
                new ParameterExpression[from.Parameters.Count];
            for (int i = 0; i < newParams.Length; i++)
            {
                newParams[i] = Expression.Parameter(toType, from.Parameters[i].Name);
                parameterMap[from.Parameters[i]] = newParams[i];
            }

            //  rebuild the lambda
            var body = new ConverterExpressionVisitor(parameterMap, toType).Visit(from.Body);

            return(Expression.Lambda(body, newParams));
        }