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例外は、プロパティが null 値にマップされた場合にのみ発生します。マッピングを行う方法は次のとおりです。

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

スローされる例外は、他の 2 つの AutoMapperMappingException をラップする AutoMapperMappingException です。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.Val1との両方s.Val2が false の場合)。マッピングが null 以外の値を持つ列挙型に解決される場合、例外は発生しません。

これは AutoMapper の別のバグですか?

4

0 に答える 0