コード例 #1
0
        public Expression MapExpression(IConfigurationProvider configurationProvider, ProfileMap profileMap, PropertyMap propertyMap, Expression sourceExpression, Expression destExpression, Expression contextExpression)
        {
            var listType = typeof(List <>).MakeGenericType(TypeHelper.GetElementType(destExpression.Type));
            var list     = CollectionMapperExtensions.MapCollectionExpression(configurationProvider, profileMap, propertyMap, sourceExpression, Default(listType), contextExpression, _ => Constant(false), typeof(List <>), CollectionMapperExtensions.MapItemExpr);
            var dest     = Variable(listType, "dest");

            return(Block(new[] { dest }, Assign(dest, list), Condition(NotEqual(dest, Default(listType)), New(destExpression.Type.GetConstructors().First(), dest), Default(destExpression.Type))));
        }
コード例 #2
0
 public Expression MapExpression(IConfigurationProvider configurationProvider, ProfileMap profileMap, PropertyMap propertyMap, Expression sourceExpression, Expression destExpression, Expression contextExpression)
 {
     if (destExpression.Type.IsInterface())
     {
         var listType = typeof(List <>).MakeGenericType(TypeHelper.GetElementType(destExpression.Type));
         destExpression = Default(listType);
     }
     return(CollectionMapperExtensions.MapCollectionExpression(configurationProvider, profileMap, propertyMap, sourceExpression,
                                                               destExpression, contextExpression, IfEditableList, typeof(List <>), CollectionMapperExtensions.MapItemExpr));
 }
コード例 #3
0
ファイル: ArrayMapper.cs プロジェクト: ithanshui/AutoMapper
        public Expression MapExpression(IConfigurationProvider configurationProvider, ProfileMap profileMap, PropertyMap propertyMap, Expression sourceExpression, Expression destExpression, Expression contextExpression)
        {
            var sourceElementType = TypeHelper.GetElementType(sourceExpression.Type);
            var destElementType   = TypeHelper.GetElementType(destExpression.Type);

            var ifNullExpr = profileMap.AllowNullCollections
                                 ? (Expression)Constant(null, destExpression.Type)
                                 : NewArrayBounds(destElementType, Constant(0));

            ParameterExpression itemParam;
            var itemExpr = CollectionMapperExtensions.MapItemExpr(configurationProvider, profileMap, propertyMap, sourceExpression.Type, destExpression.Type, contextExpression, out itemParam);

            //var count = source.Count();
            //var array = new TDestination[count];

            //int i = 0;
            //foreach (var item in source)
            //    array[i++] = newItemFunc(item, context);
            //return array;

            var countParam = Parameter(typeof(int), "count");
            var arrayParam = Parameter(ifNullExpr.Type, "destinationArray");
            var indexParam = Parameter(typeof(int), "destinationArrayIndex");

            var actions    = new List <Expression>();
            var parameters = new List <ParameterExpression> {
                countParam, arrayParam, indexParam
            };

            var countMethod = typeof(Enumerable)
                              .GetTypeInfo()
                              .DeclaredMethods
                              .Single(mi => mi.Name == "Count" && mi.GetParameters().Length == 1)
                              .MakeGenericMethod(sourceElementType);

            actions.Add(Assign(countParam, Call(countMethod, sourceExpression)));
            actions.Add(Assign(arrayParam, NewArrayBounds(destElementType, countParam)));
            actions.Add(Assign(indexParam, Constant(0)));
            actions.Add(ForEach(sourceExpression, itemParam,
                                Assign(ArrayAccess(arrayParam, PostIncrementAssign(indexParam)), itemExpr)
                                ));
            actions.Add(arrayParam);

            var mapExpr = Block(parameters, actions);

            // return (source == null) ? ifNullExpr : Map<TSourceElement, TDestElement>(source, context);
            return(Condition(Equal(sourceExpression, Constant(null)), ifNullExpr, mapExpr));
        }
コード例 #4
0
 public Expression MapExpression(TypeMapRegistry typeMapRegistry, IConfigurationProvider configurationProvider, PropertyMap propertyMap, Expression sourceExpression, Expression destExpression, Expression contextExpression)
 {
     return(Expression.Call(null, MapMethodInfo.MakeGenericMethod(sourceExpression.Type, destExpression.Type), sourceExpression, Expression.Constant(CollectionMapperExtensions.Constructor(destExpression.Type))));
 }
コード例 #5
0
 public Expression MapExpression(IConfigurationProvider configurationProvider, ProfileMap profileMap, PropertyMap propertyMap, Expression sourceExpression, Expression destExpression, Expression contextExpression)
 =>
 CollectionMapperExtensions.MapCollectionExpression(configurationProvider, profileMap, propertyMap,
                                                    Call(MembersDictionaryMethodInfo, sourceExpression, Constant(profileMap)), destExpression, contextExpression, _ => null,
                                                    typeof(Dictionary <,>), CollectionMapperExtensions.MapKeyPairValueExpr);
コード例 #6
0
 public Expression MapExpression(IConfigurationProvider configurationProvider, ProfileMap profileMap, PropertyMap propertyMap, Expression sourceExpression, Expression destExpression, Expression contextExpression)
 =>
 CollectionMapperExtensions.MapCollectionExpression(configurationProvider, profileMap, propertyMap, sourceExpression, destExpression,
                                                    contextExpression, CollectionMapperExtensions.IfNotNull, typeof(Dictionary <,>), CollectionMapperExtensions.MapItemExpr);