I can't seem to figure out why AutoMapper is throwing an exception for this mapping:
public class MyDestinationType
{
public MyCustomEnum? PropName { get; set; }
}
public enum MyCustomEnum
{
Val1,
Val2
}
Mapper.CreateMap<MySourceType, MyDestinationType>()
.ForMember(d => d.PropName, o => o.ResolveUsing(s =>
{
if (s.Val1) return MyCustomEnum.Val1;
else if (s.Val2) return MyCustomEnum.Val2;
return null;
}))
;
The exception only occurs when the MyCustomEnum? PropName
property gets mapped to a null value. Here is how I am doing the mapping:
var source = MethodToGetSourceObject();
var destination = Mapper.Map<MyDestinationType>(source);
The exception thrown is an AutoMapperMappingException which wraps 2 other AutoMapperMappingExceptions. The 4th and final InnerException is a NullReferenceException. The stack trace looks something like this:
[NullReferenceException: Object reference not set to an instance of an object.]
AutoMapper.Mappers.EnumMapper.Map(ResolutionContext context, IMappingEngineRunner mapper) +198
AutoMapper.MappingEngine.AutoMapper.IMappingEngineRunner.Map(ResolutionContext context) +447
[AutoMapperMappingException: Trying to map System.Object to System.Nullable`1[[MyProject.MyCustomEnum, MyProject, Version=2.0.4784.16259, Culture=neutral, PublicKeyToken=null]].
Using mapping configuration for MyProject.MySourceType to MyProject.MyDestinationType
Destination property: PropName
Exception of type 'AutoMapper.AutoMapperMappingException' was thrown.]
AutoMapper.MappingEngine.AutoMapper.IMappingEngineRunner.Map(ResolutionContext context) +519
AutoMapper.Mappers.PropertyMapMappingStrategy.MapPropertyValue(ResolutionContext context, IMappingEngineRunner mapper, Object mappedObject, PropertyMap propertyMap) +592
[AutoMapperMappingException: Trying to map System.Object to System.Nullable`1[[MyProject.MyCustomEnum, MyProject, Version=2.0.4784.16259, Culture=neutral, PublicKeyToken=null]].
Using mapping configuration for MyProject.MySourceType to MyProject.MyDestinationType
Destination property: PropName
Exception of type 'AutoMapper.AutoMapperMappingException' was thrown.]
AutoMapper.Mappers.PropertyMapMappingStrategy.MapPropertyValue(ResolutionContext context, IMappingEngineRunner mapper, Object mappedObject, PropertyMap propertyMap) +689
AutoMapper.Mappers.PropertyMapMappingStrategy.Map(ResolutionContext context, IMappingEngineRunner mapper) +275
AutoMapper.Mappers.TypeMapMapper.Map(ResolutionContext context, IMappingEngineRunner mapper) +273
AutoMapper.MappingEngine.AutoMapper.IMappingEngineRunner.Map(ResolutionContext context) +447
[AutoMapperMappingException: Trying to map MyProject.MySourceType to MyProject.MyDestinationType.
Using mapping configuration for MyProject.MySourceType to MyProject.MyDestinationType
Exception of type 'AutoMapper.AutoMapperMappingException' was thrown.]
AutoMapper.MappingEngine.AutoMapper.IMappingEngineRunner.Map(ResolutionContext context) +519
AutoMapper.MappingEngine.Map(Object source, Type sourceType, Type destinationType, Action`1 opts) +192
AutoMapper.MappingEngine.Map(Object source, Type sourceType, Type destinationType) +196
AutoMapper.MappingEngine.Map(Object source) +169
AutoMapper.Mapper.Map(Object source) +107
[Here is where my code calls Mapper.Map<MyDestinationType>(source)]
Again, the exception only occurs when the resolution would have PropName == null
(for example if both s.Val1
and s.Val2
were false). The exception does not occur when the mapping would resolve to the enum having some non-null value.
Is this another bug in AutoMapper?