AutoMapper,对象映射的简单使用
using AutoMapper; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace MyMapperTest { class Program { static void Main(string[] args) { Action<IMapperConfigurationExpression> configurationExpress = createMapperConfigurationExpress(); //Mapper.Initialize(c => configurationExpress(c)); //以下代码和第二行的Mapper.Initialize(c => configurationExpress(c));作用相同 Mapper.Initialize(cfg => { cfg.CreateMap<AddressDto, Address>(); cfg.CreateMap<Address, AddressDto>(); }); //单个映射实例 //Mapper.Initialize(p => p.CreateMap<AddressDto, Address>()); AddressDto dto = new AddressDto { Country = "China", City = "ShangHai", Street = "JinZhong Street" }; Address address = Mapper.Map<AddressDto, Address>(dto); AddressDto dto2 = Mapper.Map<Address, AddressDto>(address); } public static Action<IMapperConfigurationExpression> createMapperConfigurationExpress() { return new Action<IMapperConfigurationExpression>(cfg => { cfg.CreateMap<AddressDto, Address>(); cfg.CreateMap<Address, AddressDto>(); } ); } } public class AddressDto { public string Country { get; set; } public string City { get; set; } public string Street { get; set; } } public class Address { public string Country { get; set; } public string City { get; set; } public string Street { get; set; } } }