public ProposedMap CreateMapProposal(TypePair pair, MemberOptions options = null, LambdaExpression customMappingExpression = null, params Type[] parameters)
            {
                var map = new ProposedMap(pair.SourceType, pair.DestinationType, this.mapper, this.options);

                foreach (var param in parameters)
                {
                    map.ParameterTypes.Add(param);
                }

                CustomMapping customMapping = null;

                customMapping = GetCustomMappingFromExpression(pair, customMappingExpression, customMapping);

                TryGetCustomMapping(pair, out customMapping);

                var mapping = GetComplexTypeMapping(0, pair, options ?? mapper.DefaultMemberOptions, customMapping, true);

                if (mapping.CustomMapping == null)
                {
                    mapping.CustomMapping = customMapping;
                }

                map.ProposedTypeMapping = mapping;

                return(map);
            }
            public ProposedMap <TSource, TDestination, TParam> CreateMapProposal <TSource, TDestination, TParam>(MemberOptions options = null, Expression <Func <TSource, TParam, object> > customMappingExpression = null)
            {
                var map = new ProposedMap <TSource, TDestination, TParam>(this.mapper, this.options);

                map.ParameterTypes.Add(typeof(TParam));

                var pair = new TypePair(typeof(TSource), typeof(TDestination));

                CustomMapping customMapping = null;

                //if (customMappingExpression != null)
                //{
                //  customMapping = CustomMapping.GetCustomMapping(typeof(TDestination), customMappingExpression);
                //  customMappingCache[pair] = customMapping;
                //}

                customMapping = GetCustomMappingFromExpression(pair, customMappingExpression, customMapping);


                TryGetCustomMapping(pair, out customMapping);

                var mapping = GetComplexTypeMapping(0, pair, options ?? mapper.DefaultMemberOptions, customMapping, true);


                if (mapping.CustomMapping == null)
                {
                    mapping.CustomMapping = customMapping;
                }

                map.ProposedTypeMapping = mapping;

                return(map);
            }
 public CompiledMapGenerator(IMemberMapper mapper, ProposedMap map, MapperOptions options)
 {
     this.mapper = mapper;
       this.proposedMap = map;
       this.mapProcessor = new MapProposalProcessor(mapper);
       this.newParameters = new List<ParameterExpression>();
       this.options = options;
 }
        public Delegate GenerateMappingFunction(ProposedMap proposedMap)
        {
            var destination = Expression.Parameter(proposedMap.DestinationType, "destination");
              var source = Expression.Parameter(proposedMap.SourceType, "source");

              var assignments = new List<Expression>();

              var newParams = new List<ParameterExpression>();

              Expression condition;

              if (typeof(IEnumerable).IsAssignableFrom(proposedMap.SourceType)
            && typeof(IEnumerable).IsAssignableFrom(proposedMap.DestinationType))
              {

            proposedMap.ProposedTypeMapping = new ProposedTypeMapping
            {
              ProposedTypeMappings = new List<ProposedTypeMapping>
              {
            proposedMap.ProposedTypeMapping,
              },
              IsEnumerable = true,
            };
            condition = Expression.Constant(true);
              }
              else
              {
            condition = Expression.NotEqual(source, Expression.Constant(null));
              }

              BuildTypeMappingExpressions(source, destination, proposedMap.ProposedTypeMapping, assignments, newParams, proposedMap.CustomMapping);

              var block = Expression.Block(assignments);

              var conditionCheck = Expression.IfThen(condition, block);

              var outerBlock = Expression.Block(newParams, conditionCheck, destination);

              var funcType = typeof(Func<,,>).MakeGenericType(proposedMap.SourceType, proposedMap.DestinationType, proposedMap.DestinationType);

              var lambda = Expression.Lambda
              (
            funcType,
            outerBlock,
            source, destination
              );

              return CompileExpression(proposedMap.SourceType, proposedMap.DestinationType, lambda);
        }
示例#5
0
        public LambdaExpression GetProjection(ProposedMap map)
        {
            sourceParam = Expression.Parameter(map.SourceType, "source");

            var initRoot = BuildProjectionExpression(sourceParam, map.DestinationType, map.ProposedTypeMapping);

            var funcType = typeof(Func <,>).MakeGenericType(map.SourceType, map.DestinationType);

            var lambda = Expression.Lambda
                         (
                funcType,
                initRoot,
                sourceParam
                         );

            lambda = (LambdaExpression)processor.Process(lambda);

            return(lambda);
        }
        public LambdaExpression GetProjection(ProposedMap map)
        {
            sourceParam = Expression.Parameter(map.SourceType, "source");

              var initRoot = BuildProjectionExpression(sourceParam, map.DestinationType, map.ProposedTypeMapping);

              var funcType = typeof(Func<,>).MakeGenericType(map.SourceType, map.DestinationType);

              var lambda = Expression.Lambda
              (
            funcType,
            initRoot,
            sourceParam
              );

              lambda = (LambdaExpression)processor.Process(lambda);

              return lambda;
        }
 public IMapGenerator GetGenerator(IMemberMapper mapper, ProposedMap proposedMap, MapperOptions options)
 {
     return new CompiledMapGenerator(mapper, proposedMap, options);
 }
 public IMapGenerator GetGenerator(IMemberMapper mapper, ProposedMap proposedMap, MapperOptions options)
 {
     return(new CompiledMapGenerator(mapper, proposedMap, options));
 }
示例#9
0
        /// <summary>
        /// Checks if the mapper repository contains a map and if so, returns it as an out parameter.
        /// </summary>
        /// <returns></returns>
        public bool TryGetMap <TSource, TDestination>(IMemberMapper mapper, MemberOptions options, out ProposedMap <TSource, TDestination> map)
        {
            MapFuncWrapper action;

            if (cache.TryGetValue(new TypePair(typeof(TSource), typeof(TDestination)), out action))
            {
                lock (action)
                {
                    if (action.InUse)
                    {
                        map = null;
                        return(false);
                    }

                    try
                    {
                        action.InUse = true;
                        map          = (ProposedMap <TSource, TDestination>)action.CreateMapFunction(mapper, options);
                    }
                    finally
                    {
                        action.InUse = false;
                    }
                    return(true);
                }
            }

            map = null;

            return(false);
        }
示例#10
0
        /// <summary>
        /// Checks if the mapper repository contains a map and if so, returns it as an out parameter.
        /// </summary>
        /// <returns></returns>
        public bool TryGetMap(IMemberMapper mapper, MemberOptions options, TypePair pair, out ProposedMap map)
        {
            MapFuncWrapper action;

            if (cache.TryGetValue(pair, out action))
            {
                lock (action)
                {
                    if (action.InUse)
                    {
                        map = null;
                        return(false);
                    }

                    try
                    {
                        action.InUse = true;
                        map          = action.CreateMapFunction(mapper, options);
                    }
                    finally
                    {
                        action.InUse = false;
                    }
                    return(true);
                }
            }

            map = null;

            return(false);
        }
 private static Type GetMatchingFuncOverload(ProposedMap map)
 {
     switch (map.ParameterTypes.Count)
       {
     case 0:
       return typeof(Func<,,>).MakeGenericType(map.SourceType, map.DestinationType, map.DestinationType);
     case 1:
       return typeof(Func<,,,>).MakeGenericType(map.SourceType, map.DestinationType, map.ParameterTypes[0], map.DestinationType);
     case 2:
       return typeof(Func<,,,,>).MakeGenericType(map.SourceType, map.DestinationType, map.ParameterTypes[0], map.ParameterTypes[1], map.DestinationType);
     case 3:
       return typeof(Func<,,,,,,>).MakeGenericType(map.SourceType, map.DestinationType, map.ParameterTypes[0], map.ParameterTypes[1], map.ParameterTypes[2], map.DestinationType);
     default:
       throw new InvalidOperationException("No matching generic overload for Func found");
       }
 }