AutoMapper null source value and custom type conve

2019-06-27 09:37发布

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条回答
Rolldiameter
2楼-- · 2019-06-27 10:25

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;
    }
查看更多
登录 后发表回答