AutoMapper 9.0 简单用法
AutoMapper.MapperConfiguration cfg = new AutoMapper.MapperConfiguration(q => { q.CreateMap(typeof(DTO), typeof(Model)); q.CreateMap(typeof(AddressDTO), typeof(AddressModel)); }); DTO o = new DTO() { userName = "AAA" }; o.address = new AddressDTO() { country = "China" }; var m= cfg.CreateMapper().Map<Model>(o); Console.WriteLine(m.address?.country);
public class DTO { public string userName { set; get; } public string age { set; get; } public string job { set; get; } public AddressDTO address { set; get; } } public class AddressDTO { public string country { set; get; } public string province { set; get; } } // Model public class Model { public string userName { set; get; } public string age { set; get; } public string job { set; get; } public AddressModel address { set; get; } } public class AddressModel { public string country { set; get; } public string province { set; get; } }