How can I ignore mapping if property type is different with same property name? By default it\'s throwing error.
Mapper.CreateMap
You can use ForAllMembers()
to setup the appropriate mapping condition:
Mapper.Initialize(cfg =>
{
cfg.CreateMap().ForAllMembers(memberConf =>
{
memberConf.Condition((ResolutionContext cond) => cond.DestinationType == cond.SourceType);
});
}
You can also apply it globally using ForAllMaps()
:
Mapper.Initialize(cfg =>
{
// register your maps here
cfg.CreateMap();
cfg.ForAllMaps((typeMap, mappingExpr) =>
{
var ignoredPropMaps = typeMap.GetPropertyMaps();
foreach (var map in ignoredPropMaps)
{
var sourcePropInfo = map.SourceMember as PropertyInfo;
if (sourcePropInfo == null) continue;
if (sourcePropInfo.PropertyType != map.DestinationPropertyType)
map.Ignore();
}
});
});