using AutoMapper;
using System.Collections.Generic;
namespace AutoMapperTests
{
public class AutoMapperTester
{
public class Source
{
public ICollection<string> Collection { get; set; }
}
public class Destination
{
public ICollection<string> Collection { get; set; }
}
public class MergeCollectionResolver : IValueResolver<Source, Destination, ICollection<string>>
{
public ICollection<string> Resolve(Source source, Destination destination, ICollection<string> destMember, ResolutionContext context)
{
foreach (string sourceItem in source.Collection)
{
destMember.Add(sourceItem);
}
return destMember;
}
}
public void Test()
{
Mapper.Initialize(cfg =>
{
cfg.CreateMap<Source, Destination>()
.ForMember(s => s.Collection, opt => opt.ResolveUsing(new MergeCollectionResolver()));
});
var source = new Source()
{
Collection = new List<string>() { "a", "b", "c" }
};
var destination = new Destination()
{
Collection = new List<string>() { "d", "e", "f" }
};
Mapper.Map<Source, Destination>(source, destination);
// After mapping the destination collection has 0 items.
// It should have 6.
System.Diagnostics.Debug.Assert(destination.Collection.Count == 6);
}
}
}