【问题标题】:C# AutoMapper exception when mapping nullable byte to enum将可空字节映射到枚举时出现 C# AutoMapper 异常
【发布时间】:2013-04-11 19:06:04
【问题描述】:

我正在使用 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"}

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

Mapper.AssertConfigurationIsValid() 没有问题。

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

有什么想法吗?谢谢

【问题讨论】:

    标签: c# .net exception enums automapper


    【解决方案1】:

    尝试使用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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-04-14
      • 1970-01-01
      • 2020-01-08
      • 1970-01-01
      • 1970-01-01
      • 2022-01-26
      • 1970-01-01
      相关资源
      最近更新 更多