Given the following:
var object1 = new A() { Equipment = new List<FooEquipment>() { new FooEquipment() } };
var object2 = new B() { Equipment = new List<FooEquipment>() { new FooEquipment() } );
var list = new List<Foo>() { object1, object2 };
(I've inherited some legacy code, the List should be in the base class of A and B).
With some new classes,
public class AFooEquipment : FooEquipment
{
}
public class BFooEquipment : FooEquipment
{
}
Can I use AutoMapper to convert these FooEquipments into AFooEquipment and BFooEquipment by inspecting their parent item somehow? I.e. In the mapper I would see object1 is in a typeof(A) class, so I convert its equipment -> List<AFooEquipment>. It would see object2 is in a typeof(B) class, so I convert its equipment -> List<BFooEquipment>.
Is that possible?
I am using AutoMapper 3 so this may not work for you, but one option would be to use the
ResolutionContextandConstructUsingmethod, something like this:If you then call
Mapper.Map<IEnumerable<Foo>>(list)it should give you what you want. Or what I suspect you want :)