// 定义源和目标类型
public class Source
{
public int? Value { get; set; }
}
public class Destination
{
public int? Value { get; set; }
public Child Child { get; set; }
}
public class Child
{
public string Name { get; set; }
}
// 配置映射规则
var config = new MapperConfiguration(cfg =>
{
cfg.CreateMap()
.ForMember(dest => dest.Child, opt => opt.Ignore()); // 忽略Child属性的映射
});
// 使用配置映射对象
var source = new Source { Value = null };
var destination = new Destination { Value = 42, Child = new Child { Name = "bob" } };
var mapper = config.CreateMapper();
mapper.Map(source, destination);
// 输出目标对象
Console.WriteLine(destination.Value); // 42
Console.WriteLine(destination.Child.Name); // "bob"