在 Automapper 中,有时需要映射一个对象的某些属性,但不需要映射另一个属性,例如路径属性。可以使用 Ignore() 方法来指定不需要映射的属性,示例如下:
public class SourceClass
{
public string Name { get; set; }
public string Path { get; set; }
}
public class DestinationClass
{
public string Name { get; set; }
public string Path { get; set; }
}
var config = new MapperConfiguration(cfg =>
{
cfg.CreateMap()
.ForMember(dest => dest.Path, opt => opt.Ignore());
});
var source = new SourceClass { Name = "Test", Path = "c:\file.txt" };
var mapper = config.CreateMapper();
var destination = mapper.Map(source);
在这个示例中,使用 ForMember() 方法和 Ignore() 方法指定忽略 Path 属性的映射。这将确保 Path 属性不会包含在映射结果中。