将可空字节映射到枚举时的C#AutoMapper异常

时间:2013-04-18 15:17:47

标签: c# .net exception enums automapper

我正在使用AutoMapper(http://automapper.org/)自动将一个对象映射到另一个对象。

源对象:

public partial class SrcObject
{
    public int Id { get; set; }

    ...

    public Nullable<byte> QcRea { get; set; }
    public Nullable<byte> QcPyg { get; set; }

    ...

}

目标对象:

[DataContract]
public class DstObject
{
    [DataMember]
    public int Id { get; set; }

    ...

    [DataMember]
    public QualityControlState QcRea { get; set; }

    [DataMember]
    public QualityControlState QcPyg { get; set; }

    ...
}

枚举:

public enum QualityControlState
{
    Ok = 0,
    Warning = 1,
    Error = 2
}

映射器:

Mapper.CreateMap<DateTime, Nullable<DateTime>>().ConstructUsing(MessageDtoConverter.ToNullable);
Mapper.CreateMap<float, Nullable<float>>().ConstructUsing(MessageDtoConverter.ToNullable);
Mapper.CreateMap<short, Nullable<short>>().ConstructUsing(MessageDtoConverter.ToNullable);
Mapper.CreateMap<QualityControlState, Nullable<byte>>().ConstructUsing(MessageDtoConverter.ToNullable);
Mapper.CreateMap<Nullable<byte>, QualityControlState>().ConstructUsing(MessageDtoConverter.ToQualityControlState);
Mapper.CreateMap<byte, Nullable<byte>>().ConstructUsing(MessageDtoConverter.ToNullable);
Mapper.CreateMap<bool, Nullable<bool>>().ConstructUsing(MessageDtoConverter.ToNullable);
Mapper.CreateMap<int, Nullable<int>>().ConstructUsing(MessageDtoConverter.ToNullable);

Mapper.CreateMap<SrcObject, DstObject>()
    .ForMember(d => d.RawResults, s => s.Ignore())
    .ForMember(d => d.CorrectedImage, s => s.Ignore())
    .ForMember(d => d.SegmentedImage, s => s.Ignore())
    .ForMember(d => d.QcReaRotationAngleVertical, s => s.Ignore())
    .ForMember(d => d.ReatoHCW, s => s.Ignore());

Mapper.AssertConfigurationIsValid();

映射方法:

public static QualityControlState ToQualityControlState(Nullable<byte> value)
{
    return (value != null) ? (QualityControlState)Enum.Parse(typeof(QualityControlState), value.ToString()) : QualityControlState.Ok;
}

public static Nullable<DateTime> ToNullable(DateTime value)
{
    return (value != null) ? value : new DateTime();
}

public static Nullable<float> ToNullable(float value)
{
    return (value != null) ? value : 0.0f;
}

public static Nullable<short> ToNullable(short value)
{
    return (value != null) ? value : (short)0;
}

public static Nullable<byte> ToNullable(QualityControlState value)
{
    return (value != null) ? (byte)value : (byte)0;
}

public static Nullable<byte> ToNullable(byte value)
{
    return (value != null) ? value : (byte)0;
}

public static Nullable<bool> ToNullable(bool value)
{
    return (value != null) ? value : false;
}

public static Nullable<int> ToNullable(int value)
{
    return (value != null) ? value : 0;
}

例外:

_innerException = 
{"Object reference not set to an instance of an object."}

base {System.Exception} =
{"\r\n\r\nMapping types:\r\nNullable`1 -> QualityControlState\r\nSystem.Nullable`1[[System.Byte, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]] -> QualityControlState\r\n\r\nDestination path:\r...

MemberName = "QcRea"

SourceType =
{Name = "Nullable`1" FullName = "System.Nullable`1[[System.Byte, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]"}

DestinationType =
{Name = "QualityControlState" FullName = "QualityControlState"}

似乎我的自定义Mapper没有调用“Nullable to enum”。

Mapper.AssertConfigurationIsValid()没有问题。

但为什么呢?我该如何转换呢?

有什么想法吗?谢谢

1 个答案:

答案 0 :(得分:3)

尝试使用TypeConverter。这样的事情应该有效:

public class QualityControlStateTypeConverter
                           : TypeConverter<Nullable<byte>, QualityControlState>
{
    protected override QualityControlState ConvertCore(Nullable<byte> source)
    {
        return (source != null)
            ? (QualityControlState)Enum.Parse(typeof(QualityControlState),
                                              source.ToString())
            : QualityControlState.Ok;
    }
}

在映射配置中使用它,如下所示:

Mapper.CreateMap<Nullable<byte>, QualityControlState>()
      .ConvertUsing(new QualityControlStateTypeConverter());

我使用此代码对其进行了测试,似乎工作正常:

var qcs = Mapper.Map<Nullable<byte>, QualityControlState>(null); // OK
qcs = Mapper.Map<Nullable<byte>, QualityControlState>(0);        // OK
qcs = Mapper.Map<Nullable<byte>, QualityControlState>(1);        // Warning
qcs = Mapper.Map<Nullable<byte>, QualityControlState>(2);        // Error
qcs = Mapper.Map<Nullable<byte>, QualityControlState>(3);        // 3