예제 #1
0
        /// <summary>
        /// 嵌套
        /// </summary>
        private static void ExampleJ()
        {
            ThreeDto a = new ThreeDto()
            {
                ID         = 1,
                entityName = "name-1"
            };

            ThreeDto aSub = new ThreeDto()
            {
                ID         = 11,
                entityName = "name-1-sub"
            };

            a.Children = new List <ThreeDto>()
            {
                aSub
            };

            var entity = MapperExt.MapTo <ThreeDto, ThreeEntity>(a);

            Console.WriteLine("Id = {0},EntityName = {1}", entity.Id, entity.EntityName);
            foreach (var item in entity.Children)
            {
                Console.WriteLine("Id = {0},EntityName = {1}", item.Id, item.EntityName);
            }
            Console.WriteLine("--------------------");

            ThreeDto[] arr = new ThreeDto[] {
                new ThreeDto {
                    ID                = 1,
                    entityName        = "name-1",
                    PrefixHandPostfix = 100
                },
                new ThreeDto {
                    ID                = 2,
                    entityName        = "name-2",
                    PrefixHandPostfix = 200
                }
            };

            var config = new MapperConfiguration(cfg =>
            {
                cfg.RecognizePrefixes("Prefix");
                cfg.RecognizePostfixes("Postfix");

                //全局属性/字段过滤
                cfg.CreateMap <ThreeDto, ThreeEntity>();
            });

            //config.AssertConfigurationIsValid();

            var list = MapperExt.MapToList <ThreeDto, ThreeEntity>(arr);

            foreach (var item in list)
            {
                Console.WriteLine("Id = {0},Hand = {1}", item.Id, item.Hand);
            }
        }
예제 #2
0
        /// <summary>
        /// 静态方法
        /// </summary>
        private static void ExampleA()
        {
            ThreeDto a = new ThreeDto()
            {
                ID = 1, Age = 11, entityName = "name1"
            };
            ThreeDto b = new ThreeDto()
            {
                ID = 2, Age = 22, entityName = "name2"
            };
            ThreeEntity aa = MapperExt.MapTo <ThreeDto, ThreeEntity>(a);

            Console.WriteLine("Id = {0},Age = {1},Name = {2}", aa.Id, aa.Age, aa.EntityName);
        }
예제 #3
0
        /// <summary>
        /// 数组和列表映射
        /// </summary>
        private static void ExampleH()
        {
            ThreeDto a = new ThreeDto()
            {
                ID         = 1,
                entityName = "name-1"
            };

            ThreeDto[] arr = new ThreeDto[] {
                new ThreeDto {
                    ID         = 1,
                    entityName = "name-1"
                },
                new ThreeDto {
                    ID         = 2,
                    entityName = "name-2"
                }
            };


            var config = new MapperConfiguration(cfg =>
            {
                //全局属性/字段过滤
                cfg.CreateMap <ThreeDto, ThreeEntity>();
            });

            var list = MapperExt.MapToList <ThreeDto, ThreeEntity>(arr);

            foreach (var item in list)
            {
                Console.WriteLine("Id = {0},Hand = {1}", item.Id, item.EntityName);
            }
            Console.WriteLine("--------new-------");

            IMapper maper = config.CreateMapper();
            //IEnumerable<Destination> ienumerableDest = mapper.Map<Source[], IEnumerable<Destination>>(sources);
            //ICollection<Destination> icollectionDest = mapper.Map<Source[], ICollection<Destination>>(sources);
            //IList<Destination> ilistDest = mapper.Map<Source[], IList<Destination>>(sources);
            //List<Destination> listDest = mapper.Map<Source[], List<Destination>>(sources);
            //Destination[] arrayDest = mapper.Map<Source[], Destination[]>(sources);

            //转为IEnumerable
            IEnumerable <ThreeEntity> arrIEnumerable = maper.Map <ThreeDto[], IEnumerable <ThreeEntity> >(arr);

            foreach (var item in arrIEnumerable)
            {
                Console.WriteLine("Id = {0},Hand = {1}", item.Id, item.EntityName);
            }
            Console.WriteLine("----------------");

            //转为ICollection
            ICollection <ThreeEntity> arrICollection = maper.Map <ThreeDto[], ICollection <ThreeEntity> >(arr);

            foreach (var item in arrICollection)
            {
                Console.WriteLine("Id = {0},Hand = {1}", item.Id, item.EntityName);
            }
            Console.WriteLine("----------------");

            //转为IList
            IList <ThreeEntity> arrIList = maper.Map <ThreeDto[], IList <ThreeEntity> >(arr);

            foreach (var item in arrIList)
            {
                Console.WriteLine("Id = {0},Hand = {1}", item.Id, item.EntityName);
            }
            Console.WriteLine("----------------");

            //转为集合
            List <ThreeEntity> arrList = maper.Map <ThreeDto[], List <ThreeEntity> >(arr);

            foreach (var item in arrList)
            {
                Console.WriteLine("Id = {0},Hand = {1}", item.Id, item.EntityName);
            }
            Console.WriteLine("----------------");

            //转为数组
            ThreeEntity[] arrT = maper.Map <ThreeDto[], ThreeEntity[]>(arr);
            foreach (var item in arrT)
            {
                Console.WriteLine("Id = {0},Hand = {1}", item.Id, item.EntityName);
            }
        }