gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 16:18:30 25 4
gpt4 key购买 nike

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

异常(exception):

_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() 没有问题。

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

有什么想法吗?谢谢

最佳答案

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

关于将可空字节映射到枚举时出现 C# AutoMapper 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16086815/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com