Dailycode.info

Short solution for short problems

Clone a reference object with automapper

If you want your business objects to implement the ICloneable interface, using the automapper to clone your object could be very easy.

Here's how I did this. I implemented the ICloneable interface on my User object. The implmentation looks like this:

public object Clone()

{

    User clone = new User();

    AutoMapper.Mapper.CreateMap<IUser, User>();

    return (User)AutoMapper.Mapper.Map(this, typeof(IUser), typeof(User));

Then just Clone it:

foreach (User u in result)

{

    _newUserList.Add((User)u.Clone());

}