AutoMapper null source value and custom type converter, fails to map?

时光总嘲笑我的痴心妄想 提交于 2020-01-14 13:07:50

问题


When using a custom type converter (ITypeConverter) with AutoMapper it does not seem to enter the type converters code if the source value is null, e.g:

Mapper.CreateMap<string, Enum>().ConvertUsing<EnumConverter>();

Assert.AreEqual(Enum.Value1, Mapper.Map<Enum>("StringValue1"));
Assert.AreEqual(Enum.Value1, Mapper.Map<Enum>(null);
Assert.AreEqual(Enum.Value1, Mapper.Map<Enum?>(null);

The type converter looks something like:

public class EnumConvertor: ITypeConverter<string, Enum>
{
    public Enum Convert(ResolutionContext context)
    {
        string value = (string) context.SourceValue;

        switch (value)
        {
            case "StringValue2":
                return Enum.Value2;
            case "StringValue3":
                return Enum.Value3;
            case "StringValue1":
            default:
                return Enum.Value1;
        }
    }
}

In the last two cases, the results are:

Assert.AreEqual(Enum.Value1, Mapper.Map<Enum>(null);

0 - Not a valid enum value

Assert.AreEqual(Enum.Value1, Mapper.Map<Enum?>(null);

Null

From debugging into the tests, in these two cases the custom TypeConverter never gets hit and it seems AutoMapper has some initial checks in the mapper to map without resorting to the TypeConverter?

If I specify an empty string ("") the test works as expected.


回答1:


Taking a quick look at the source code of automapper, Mapper.Map(object source) does a null check. If source is null it will return the default value of T:

    public TDestination Map<TDestination>(object source, Action<IMappingOperationOptions> opts)
    {
        var mappedObject = default(TDestination);
        if (source != null)
        {
            var sourceType = source.GetType();
            var destinationType = typeof(TDestination);

            mappedObject = (TDestination)Map(source, sourceType, destinationType, opts);
        }
        return mappedObject;
    }


来源:https://stackoverflow.com/questions/22378138/automapper-null-source-value-and-custom-type-converter-fails-to-map

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!