예제 #1
0
            private Expression ConvertCustomExpression(Expression node, PropertyMap propertyMap)
            {
                var replaced = new ParameterReplacementVisitor(node);
                var newBody  = replaced.Visit(propertyMap.CustomExpression.Body);

                return(newBody);
            }
        internal static Expression CreateMapExpression(IMappingEngine mappingEngine, ExpressionRequest request, Expression instanceParameter, Internal.IDictionary <ExpressionRequest, int> typePairCount)
        {
            var typeMap = mappingEngine.ConfigurationProvider.FindTypeMapFor(request.SourceType,
                                                                             request.DestinationType);

            if (typeMap == null)
            {
                const string MessageFormat = "Missing map from {0} to {1}. Create using Mapper.CreateMap<{0}, {1}>.";

                var message = string.Format(MessageFormat, request.SourceType.Name, request.DestinationType.Name);

                throw new InvalidOperationException(message);
            }

            var bindings = CreateMemberBindings(mappingEngine, request, typeMap, instanceParameter, typePairCount);

            var parameterReplacer = new ParameterReplacementVisitor(instanceParameter);
            var visitor           = new NewFinderVisitor();
            var ctorExpr          = typeMap.ConstructExpression ?? Expression.Lambda(Expression.New(request.DestinationType));

            visitor.Visit(parameterReplacer.Visit(ctorExpr));

            var expression = Expression.MemberInit(
                visitor.NewExpression,
                bindings.ToArray()
                );

            return(expression);
        }
예제 #3
0
    public static Expression <Func <TInput, TResult> > ComposeWith <TInput, TParam, TResult>(
        this Expression <Func <TParam, TResult> > left, Expression <Func <TInput, TParam> > right)
    {
        var param = left.Parameters.Single();

        var visitor = new ParameterReplacementVisitor(p => {
            if (p == param)
            {
                return(right.Body);
            }
            return(null);
        });

        return(Expression.Lambda <Func <TInput, TResult> >(
                   visitor.Visit(left.Body),
                   right.Parameters.Single()));
    }
            protected override Expression VisitMember(MemberExpression node)
            {
                var memberAccessor = node.Member.ToMemberAccessor();
                var propertyMap    = _typeMap.GetExistingPropertyMapFor(memberAccessor);


                if (propertyMap.CustomExpression != null)
                {
                    var replaced = new ParameterReplacementVisitor(_newParam);
                    var newBody  = replaced.Visit(propertyMap.CustomExpression.Body);
                    return(newBody);
                }

                var replacedExpression = Visit(node.Expression);

                return(propertyMap.GetSourceValueResolvers()
                       .OfType <IMemberGetter>()
                       .Aggregate(replacedExpression,
                                  (current, memberGetter) => Expression.MakeMemberAccess(current, memberGetter.MemberInfo)));
            }
예제 #5
0
            protected override Expression VisitMember(MemberExpression node)
            {
                var memberAccessor = node.Member.ToMemberAccessor();
                var propertyMap = _typeMap.GetExistingPropertyMapFor(memberAccessor);


                if (propertyMap.CustomExpression != null)
                {
                    var replaced = new ParameterReplacementVisitor(_newParam);
                    var newBody = replaced.Visit(propertyMap.CustomExpression.Body);
                    return newBody;
                }

                var replacedExpression = Visit(node.Expression);

                return propertyMap.GetSourceValueResolvers()
                    .OfType<IMemberGetter>()
                    .Aggregate(replacedExpression,
                        (current, memberGetter) => Expression.MakeMemberAccess(current, memberGetter.MemberInfo));
            }
예제 #6
0
        public Expression CreateMapExpression(ExpressionRequest request,
            Expression instanceParameter, Internal.IDictionary<ExpressionRequest, int> typePairCount)
        {
            var typeMap = ConfigurationProvider.ResolveTypeMap(request.SourceType,
                request.DestinationType);

            if (typeMap == null)
            {
                const string MessageFormat = "Missing map from {0} to {1}. Create using Mapper.CreateMap<{0}, {1}>.";

                var message = string.Format(MessageFormat, request.SourceType.Name, request.DestinationType.Name);

                throw new InvalidOperationException(message);
            }

            var bindings = CreateMemberBindings(request, typeMap, instanceParameter, typePairCount);

            var parameterReplacer = new ParameterReplacementVisitor(instanceParameter);
            var visitor = new NewFinderVisitor();
            var constructorExpression = typeMap.DestinationConstructorExpression(instanceParameter);
            visitor.Visit(parameterReplacer.Visit(constructorExpression));

            var expression = Expression.MemberInit(
                visitor.NewExpression,
                bindings.ToArray()
                );
            return expression;
        }