Skip to content

Instantly share code, notes, and snippets.

@greendimka
Last active May 20, 2019 08:48
Show Gist options
  • Save greendimka/4fa78aeb37938f46343cfd3e4cc1f028 to your computer and use it in GitHub Desktop.
Save greendimka/4fa78aeb37938f46343cfd3e4cc1f028 to your computer and use it in GitHub Desktop.
class Program
{
static void Main(string[] args)
{
var config = new MapperConfiguration(c =>
{
c.CreateMap<SourceClass, TargetClass>()
.ForMember(x => x.Vals, o => o.MapFrom((srcCls, trgCls) =>
{
trgCls.Vals.Clear();
trgCls.Vals.AddRange(srcCls.Vals);
return trgCls.Vals;
}));
});
config.AssertConfigurationIsValid();
IMapper mapper = new Mapper(config);
var src = new SourceClass();
var trg = mapper.Map<TargetClass>(src);
Console.WriteLine(string.Join('|', trg.Vals));
}
}
class SourceClass
{
public SourceClass()
{
Vals = new List<int> { 1, 2, 3 };
}
public List<int> Vals { get; set; }
}
class TargetClass
{
private List<int> _vals = new List<int>();
public List<int> Vals => _vals;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment