Enums 将可为null的枚举映射到null时自动映射引发NullReferenceException

Enums 将可为null的枚举映射到null时自动映射引发NullReferenceException,enums,automapper,nullable,nullreferenceexception,automapper-2,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.Prop

我似乎不明白为什么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;
    }))
;
引发的异常是一个AutoMapperMappingException,它封装了另外两个AutoMapperMappingException。第四个也是最后一个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)]
[NullReferenceException:对象引用未设置为对象的实例。]
AutoMapper.Mappers.EnumMapper.Map(ResolutionContext上下文,IMappingEngineRunner映射器)+198
AutoMapper.MappingEngine.AutoMapper.IMappingEngineRunner.Map(ResolutionContext上下文)+447
[AutoMapperMappingException:尝试将System.Object映射到System.Nullable`1[[MyProject.MyCustomEnum,MyProject,Version=2.0.4784.16259,Culture=neutral,PublicKeyToken=null]]。
使用MyProject.MySourceType到MyProject.MyDestinationType的映射配置
目标属性:PropName
引发了类型为“AutoMapper.AutoMappingException”的异常。]
AutoMapper.MappingEngine.AutoMapper.IMappingEngineRunner.Map(ResolutionContext上下文)+519
AutoMapper.Mappers.PropertyMappingStrategy.MapPropertyValue(ResolutionContext上下文、IMappingEngineRunner映射器、Object mappedObject、PropertyMap PropertyMap)+592
[AutoMapperMappingException:尝试将System.Object映射到System.Nullable`1[[MyProject.MyCustomEnum,MyProject,Version=2.0.4784.16259,Culture=neutral,PublicKeyToken=null]]。
使用MyProject.MySourceType到MyProject.MyDestinationType的映射配置
目标属性:PropName
引发了类型为“AutoMapper.AutoMappingException”的异常。]
AutoMapper.Mappers.PropertyMappingStrategy.MapPropertyValue(ResolutionContext上下文、IMappingEngineRunner映射器、Object mappedObject、PropertyMap PropertyMap)+689
AutoMapper.Mappers.PropertyMappingStrategy.Map(ResolutionContext上下文,IMappingEngineRunner mapper)+275
AutoMapper.Mappers.typemapper.Map(ResolutionContext上下文,IMappingEngineRunner映射器)+273
AutoMapper.MappingEngine.AutoMapper.IMappingEngineRunner.Map(ResolutionContext上下文)+447
[AutoMapperMappingException:尝试将MyProject.MySourceType映射到MyProject.MyDestinationType。
使用MyProject.MySourceType到MyProject.MyDestinationType的映射配置
引发了类型为“AutoMapper.AutoMappingException”的异常。]
AutoMapper.MappingEngine.AutoMapper.IMappingEngineRunner.Map(ResolutionContext上下文)+519
AutoMapper.MappingEngine.Map(对象源,类型sourceType,类型destinationType,操作'1 opts)+192
AutoMapper.MappingEngine.Map(对象源,类型sourceType,类型destinationType)+196
AutoMapper.MappingEngine.Map(对象源)+169
AutoMapper.Mapper.Map(对象源)+107
[这里是我的代码调用Mapper.Map(源代码)的地方]
同样,只有当解析将具有
PropName==null
时才会发生异常(例如,如果
s.Val1
s.Val2
都为false)。当映射解析为具有某些非空值的枚举时,不会发生异常


这是AutoMapper中的另一个bug吗?

您找到解决方案了吗?我遇到了同样的问题!?
[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)]