public class CustomResolver : IValueResolver
{
public ChildDestination Resolve(Source source, Destination destination, ChildDestination destMember, ResolutionContext context)
{
// implement your custom logic here to map properties from Source to ChildDestination
return new ChildDestination();
}
}
然后,在配置映射时,我们可以使用这个自定义解析器来映射ChildDestination类型:
// configure mapping from Source to Destination
var configuration = new MapperConfiguration(cfg =>
{
cfg.CreateMap()
.ForMember(dest => dest.Child, opt => opt.MapFrom(src => src.ChildDestination))
.ForMember(dest => dest.AnotherChild, opt => opt.MapFrom(src => src.AnotherChildDestination));
});
// configure mapping from ChildSource to ChildDestination using custom resolver
configuration.CreateMap().ForMember(dest => dest.ChildProperty, opt => opt.MapFrom());
// create a mapper
var mapper = configuration.CreateMapper();
// use mapper to map objects
var source = new Source();
var dest = mapper.Map(source);