public static void Main()
var foo = new Foo { Id = 1, Name = "Foo" };
var dto = MyMapper.GetMapper().Map<Foo, FooDto>(foo);
dto = new FooDto { Id = 2, Name = "DTO", Unmapped = "Unmapped" };
foo = MyMapper.GetMapper().Map<FooDto, Foo>(dto);
public static class MyMapper
public static IMapper GetMapper()
var mapperConfig = new MapperConfiguration(
configuration.CreateMap<Foo, FooDto>().ReverseMap();
configuration.IgnoreUnmapped();
mapperConfig.AssertConfigurationIsValid();
return mapperConfig.CreateMapper();
public int Id { get; set; }
public string Name { get; set; }
public int Id { get; set; }
public string Name { get; set; }
public string Unmapped { get; set; }
public static class MapperExtensions
private static void IgnoreUnmappedProperties(TypeMap map, IMappingExpression expr)
foreach (string propName in map.GetUnmappedPropertyNames())
var srcPropInfo = map.SourceType.GetProperty(propName);
expr.ForSourceMember(propName, opt => opt.Ignore());
var destPropInfo = map.DestinationType.GetProperty(propName);
if (destPropInfo != null)
expr.ForMember(propName, opt => opt.Ignore());
public static void IgnoreUnmapped(this IProfileExpression profile)
profile.ForAllMaps(IgnoreUnmappedProperties);
public static void IgnoreUnmapped(this IProfileExpression profile, Func<TypeMap, bool> filter)
profile.ForAllMaps((map, expr) =>
IgnoreUnmappedProperties(map, expr);
public static void IgnoreUnmapped(this IProfileExpression profile, Type src, Type dest)
profile.IgnoreUnmapped((TypeMap map) => map.SourceType == src && map.DestinationType == dest);
public static void IgnoreUnmapped<TSrc, TDest>(this IProfileExpression profile)
profile.IgnoreUnmapped(typeof(TSrc), typeof(TDest));