我正在使用 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.AssertConfigurationIsValid() 没有问题。
但为什么?我该如何转换?
有任何想法吗?谢谢