我在我的项目中使用 Automapper 将业务实体映射到 DTO。
public class TransportStop
{
public Point[] Points { get; set; }
}
public class TransportStopDto
{
public PointDto[] Points { get; set; }
public TransportStopDto()
{
Points = new PointDto[0];
}
}
在构造函数中,我使用空数组初始化 Points 属性,以确保它始终不为空。我正在使用基本配置进行映射。
Mapper.CreateMap<Point, PointDto>();
Mapper.CreateMap<TransportStop, TransportStopDto>();
TransportStop stop = new TransportStop()
{
Points = new Point[]
{
new Point() { X = 1, Y = 1 },
new Point() { X = 2, Y = 2 }
}
};
TransportStopDto dto = Mapper.Map<TransportStop, TransportStopDto>(stop);
使用 Automapper 2.0.0 它工作得很好,但是在升级到 2.2.0 版本后,我得到了带有内部异常的映射异常:
指数数组的边界之外
似乎 Automapper 试图映射数组的每个成员,而不是覆盖整个数组。如果我从构造函数中删除属性初始化并将其保留为空,则一切正常。
是否可以将 Automapper 2.2.0 配置为始终用新的数组属性覆盖现有的数组属性?