Пример #1
0
        public Expression MapExpression(IConfigurationProvider configurationProvider, ProfileMap profileMap,
                                        PropertyMap propertyMap, Expression sourceExpression, Expression destExpression, Expression contextExpression)
        {
            Expression mapExpr = null;

            if (IsDataReader(sourceExpression.Type, destExpression.Type))
            {
                ParameterExpression itemParam;
                Expression          itemExpr;

                try
                {
                    itemExpr = CollectionMapperExpressionFactory.MapItemExpr(configurationProvider, profileMap, typeof(IEnumerable <IDataRecord>), destExpression.Type, contextExpression, out itemParam);
                }
                catch (Exception ex)
                {
                    throw new AutoMapperMappingException("Missing type map configuration or unsupported mapping.", ex, new TypePair(sourceExpression.Type, destExpression.Type));
                }

                if (YieldReturnEnabled)
                {
                    var        mapFunc             = Lambda(itemExpr, itemParam, contextExpression as ParameterExpression);
                    MethodInfo genericMapFunc      = DataReaderHelper.DataReaderAsYieldReturnMethod.MakeGenericMethod(TypeHelper.GetElementType(destExpression.Type));
                    var        sourceAsYieldReturn = Call(null, genericMapFunc, sourceExpression, contextExpression, mapFunc);

                    mapExpr =
                        Block(sourceAsYieldReturn);
                }
                else
                {
                    var sourceAsEnumerable = Call(null, DataReaderHelper.DataReaderAsEnumerableMethod, sourceExpression);
                    var listType           = typeof(List <>).MakeGenericType(TypeHelper.GetElementType(destExpression.Type)); // Cache this if we experience poor performance
                    var listVar            = Variable(listType, "list");
                    var listAddExpr        = Call(listVar, listType.GetMethod("Add"), itemExpr);                              // Cache this if we experience poor performance

                    mapExpr =
                        Block(new[] { listVar },
                              Assign(listVar, New(listType)),
                              ExpressionExtensions.ForEach(sourceAsEnumerable, itemParam, listAddExpr),
                              listVar);
                }
            }

            return(mapExpr);
        }