1

我知道这听起来像是重复的,但实际上我找不到有关特定主题的任何信息,因此请在判断之前先阅读它:-)

我想使用 automapper 从我无法控制的相当大的类结构中复制信息。我无法更改接口和类。

首先将此结构视为众所周知的工作模式:来源:

[DataMember]
[ProtoMember(4)]
public List<Journey> Journeys = new List<Journey>();

目的地:

[XmlArray("Journeys")]
[DataMember]
[XmlArrayItem("InventoryJourney", typeof (InventoryJourney), Namespace = "http://my.schema.com/msg.journey")]
public MsgList<InventoryJourney> Journeys
{
  get
  {
    if (this._journeys == (MsgList<InventoryJourney>) null)
      this._journeys = new MsgList<InventoryJourney>();
    return this._journeys;
  }
  set
  {
    this._journeys = value;
  }
}

InventoryJourney 是 Journey 类的直接继承。由于界面清楚地显示了要创建的类型,Automapper 可以正确处理。目的地将包含 InventoryJourney 类型的对象。

现在我的问题:

提到的 InventoryJourney 类需要填充一个在基类 Journey 中定义的属性,如下所示:

目的地:

public MsgList<Segment> Segments

资源:

public List<Segment> Segments = new List<Segment>();

该属性的签名看起来应该包含 Segment 类,但实际上该列表包含类型为 InventorySegment 的类,它是 Segment 类的直接继承。

是否可以通过创建某个派生类型而不是属性定义的基类型来配置 AutoMapper?

我当前的配置如下所示:

Mapper.CreateMap<Source.Segment, Destination.Segment>()
    .Include<Source.Segment, Destination.InventorySegment>();
Mapper.CreateMap<Source.Segment, Destination.InventorySegment>();
4

0 回答 0