将可空的枚举映射为null时,AutoMapper会抛出NullReferenceException

时间:2013-02-05 14:20:10

标签: enums automapper nullable nullreferenceexception automapper-2

我似乎无法弄清楚为什么AutoMapper会为此映射抛出异常:

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;
    }))
;

仅当MyCustomEnum? PropName属性映射到空值时才会发生异常。以下是我如何进行映射:

var source = MethodToGetSourceObject();
var destination = Mapper.Map<MyDestinationType>(source);

抛出的异常是一个AutoMapperMappingException,它包装了另外两个AutoMapperMappingExceptions。第四个也是最后一个InnerException是NullReferenceException。堆栈跟踪看起来像这样:

[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)]

同样,仅当解析具有PropName == null时(例如,如果s.Val1s.Val2均为假),则仅发生异常。当映射将解析为具有某些非空值的枚举时,不会发生异常。

这是AutoMapper中的另一个错误吗?

0 个答案:

没有答案