.ForMember(c = c.Property, options => options.Ignore())
当您使用单个对象进行映射时有效。但是,忽略在列表的情况下不起作用。例如,请参见下面的代码,其中目标列表对象已经有一些条目,并且在映射时,我想根据创建的映射忽略特定属性。当映射发生时,忽略规则不适用,值仅从源设置
//class definition
public class Customer
{
public int MyProperty { get; set; }
public string Next { get; set; }
}
public class Customer2
{
public int MyProperty { get; set; }
public string Next { get; set; }
}
//create map where Next property is to be ignored
AutoMapper.Mapper.CreateMap()
.ForMember(c=> c.Next, options => options.Ignore());
//create and populate source list and dest list
var sourceCust = new Customer() { MyProperty = 1, Next = string.Empty };
var destCust = new Customer2() { MyProperty = 0, Next = "Hrishi" };
var sourceList = new List<Customer>();
sourceList.Add(sourceCust);
var destList = new List<Customer2>();
destList.Add(destCust);
//do the mapping
destList = AutoMapper.Mapper.Map, List>(sourceList);
//现在在 destCust 实例中,我希望 Next 基于 options.ignore 属性 Next.. 保留值“Hrishi”,但它不会发生。此值设置为空。