// 假设我们有两个类,Source 和 Destination
public class Source
{
public int Id { get; set; }
public string Name { get; set; }
public IList Destinations { get; set; }
}
public class Destination
{
public int Id { get; set; }
public string Name { get; set; }
public IList DestinationIds { get; set; }
}
// 我们需要将 Source 类型映射为 Destination 类型
var config = new MapperConfiguration(cfg =>
{
cfg.CreateMap()
.ForMember(dest => dest.DestinationIds, opt => opt.MapFrom(src => src.Destinations.Select(d => d.Id)));
});
// 然后创建一个 Mapper 实例并使用 Map 方法进行映射
var source = new Source
{
Id = 1,
Name = "Source",
Destinations = new List
{
new DestinationItem { Id = 2, Name = "Destination one" },
new DestinationItem { Id = 3, Name = "Destination two" }
}
};
var mapper = config.CreateMapper();
var destination = mapper.Map(source);