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。第 4 个也是最后一个 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 中的另一个错误吗?

4

0 回答 0