static void Main(string[] args)
        {
            var links = new ItemLink[]
            {
                new ItemLink {
                    Description = "desc 1"
                },
                new ItemLink {
                    Description = "desc 2"
                },
            };
            var item = new Item
            {
                ItemLinks = links,
            };

            Mapper.Initialize(cfg =>
            {
                cfg.CreateMap <ItemLink, ItemLink>();    // you can extend this part of the configuration here
                cfg.CreateMap <Item, Item>();
                cfg.CreateMap <ItemLink, MyCustomClass>()
                .ForMember(myCustomClass => myCustomClass.DescriptionWithDifferentName,
                           expression => expression.MapFrom(itemLink => itemLink.Description));
                // to map to a different type
                // more configs can do here
                // e.g. cfg.CreateMap<Item, SomeOtherClass>();
            });
            ItemLink linkClone = Mapper.Map <ItemLink>(links[0]);

            ItemLink[]    linkArrayClone      = Mapper.Map <ItemLink[]>(item.ItemLinks);
            Item          itemClone           = Mapper.Map <Item>(item);
            MyCustomClass myCustomClassObject = Mapper.Map <MyCustomClass>(links[0]);
        }
Exemple #2
0
        static void Main(string[] args)
        {
            var links = new ItemLink[]
            {
                new ItemLink {
                    Description = "desc 1"
                },
                new ItemLink {
                    Description = "desc 2"
                },
            };
            var item = new Item
            {
                ItemLinks = links,
            };

            Mapper.Initialize(cfg =>
            {
                // now AutoMapper will try co create maps on it's own
                cfg.CreateMissingTypeMaps = true;

                // we can still add custom maps like that
                cfg.CreateMap <ItemLink, MyCustomClass>()
                .ForMember(myCustomClass => myCustomClass.DescriptionWithDifferentName,
                           expression => expression.MapFrom(itemLink => itemLink.Description));
            });
            ItemLink linkClone = Mapper.Map <ItemLink>(links[0]);

            ItemLink[] linkArrayClone = Mapper.Map <ItemLink[]>(item.ItemLinks);
            Item       itemClone      = Mapper.Map <Item>(item);
            // without custom map myCustomClassObject.DescriptionWithDifferentName would be null
            MyCustomClass myCustomClassObject = Mapper.Map <MyCustomClass>(links[0]);
        }
Exemple #3
0
 static void Main(string[] args)
 {
     var links = new ItemLink[]
     {
         new ItemLink {
             Description = "desc 1"
         },
         new ItemLink {
             Description = "desc 2"
         },
     };
     var config = new MapperConfiguration(cfg =>
     {
         cfg.CreateMap <ItemLink, ItemLink>();    // you can extend this part of the configuration here
         // more configs can go here
     });
     IMapper mapper     = new Mapper(config);
     var     clone      = mapper.Map <ItemLink>(links[0]);
     var     arrayClone = mapper.Map <ItemLink[]>(links);
 }