When I map an object with a List property Automapper by default sets the list property on the destination object to the instance from the source object.
Is there a way for automapper to create a new list and copy the items but not copy the list instance?
I would like the following test to pass:
var widget = new Widget
{
Tags = new List<string> {"big", "bright"}
};
Mapper.Reset();
Mapper.CreateMap<Widget, Widget>();
var widgetCopy = Mapper.Map<Widget, Widget>(widget);
CollectionAssert.Contains(widgetCopy.Tags, "big");
CollectionAssert.Contains(widgetCopy.Tags, "bright");
Assert.AreNotSame(widget.Tags, widgetCopy.Tags);
where the widget class looks like:
class Widget
{
public IList<string> Tags { get; set; }
}
Currently the last assert fails because the two tags properties point to the same instance of a list. This is a problem when the objects are persisted with NHibernate.