public class Source
{
public int Id { get; set; }
public string Name { get; set; }
}
public class Destination
{
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
}
public static void Main()
{
var sourceList = new List
{
new Source { Id = 1, Name = "John" },
new Source { Id = 2, Name = "Mary" }
};
// 定义映射规则
var mappingConfig = new MapperConfiguration(cfg =>
{
cfg.CreateMap()
.ForMember(dest => dest.Description, opt => opt.Ignore());
});
// 映射配置选项
var mappingOptions = new List
{
new MappingOption
{
PropertyMap = mappingConfig.FindTypeMapFor().GetPropertyMap(dest => dest.Description),
ShouldMapSourceValue = true
}
};
// 映射对象列表
var destinationList = mappingConfig.Map, List>(sourceList,
opts => opts.ConfigureMap(mappingOptions));
foreach (var destination in destinationList)
{
Console.WriteLine(destination.Name + " " + destination.Id + " " + destination.Description);
}
}