解决方法
是的,可以完成,但是您必须使用映射配置中的Condition()指定何时跳过目标属性.
这是一个例子.考虑以下类:
public class Source { public string Text { get; set; } public bool Map { get; set; } } public class Destination { public string Text { get; set; } }
第一个地图不会覆盖destination.Text,但是第二个将会.
Mapper.CreateMap<Source,Destination>() .ForMember(dest => dest.Text,opt => opt.Condition(src => src.Map)); var source = new Source { Text = "Do not map",Map = false }; var destination = new Destination { Text = "Leave me alone" }; Mapper.Map(source,destination); source.Map = true; var destination2 = new Destination { Text = "I'll be overwritten" }; Mapper.Map(source,destination2);