AutoMapper:从两个属性构建目标

时间:2013-11-19 13:04:03

标签: c# automapper

我有以下课程:

public class Person
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string[] Bookmarks { get; set; }
}

public class PersonEntry
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

public class PersonEntryWithBookmarks
{
    public PersonEntry Entry { get; set; }
    public string[] Bookmarks { get; set; } 
}

我想创建以下映射:

Mapper.Map<PersonEntryWithBookmarks, Person>(person)

如何将源中的Entry属性映射到Person中的所有属性(除了Bookmarks)和Bookmarks属性,从源到{{人物中的1}}属性?

1 个答案:

答案 0 :(得分:1)

Mapper.CreateMap<PersonEntry, Person>();
Mapper.CreateMap<PersonEntryWithBookmarks, Person>()
    .AfterMap((src, dest) => Mapper.Map<PersonEntry, Person>(src.Entry, dest));

var person = Mapper.Map<PersonEntryWithBookmarks, Person>(personEntryWithBookmarks);

将自动为您映射书签。