I know this sounds like a duplicate but actually I couldn't find anything about the specific topic, so please read it first before you judge :-)
I want to use automapper to copy information from a pretty big class structure that I do not have under my control. Interfaces and classes cannot be changed by me.
Consider this structure as a well known working pattern first: Source:
[DataMember]
[ProtoMember(4)]
public List<Journey> Journeys = new List<Journey>();
Destination:
[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 is a direct inheritation from Journey class. As the interface clearly shows which types to create, Automapper handles that correctly. The destination will contain objects of type InventoryJourney.
Now to my problem:
The mentioned InventoryJourney class needs to have a property populated that is defined in the base class Journey, looks like this:
Destination:
public MsgList<Segment> Segments
Source:
public List<Segment> Segments = new List<Segment>();
The signature of this property looks like Segment classes shall be contained, but actually this list contains classes of type InventorySegment that is a direct inheritation of Segment class.
Is it possible to configure AutoMapper in a way that it creates a certain derived type instead of the base type defined by the property?
My current configuration looks like this:
Mapper.CreateMap<Source.Segment, Destination.Segment>()
.Include<Source.Segment, Destination.InventorySegment>();
Mapper.CreateMap<Source.Segment, Destination.InventorySegment>();