如果我有一个班级,Foo,看起来像这样......
class Foo
{
public string TelephoneNumber1;
public string TelephoneNumber2;
public string TelephoneNumber3;
public string TelephoneNumber4;
}
是否可以使用AutoMapper映射到此类...
class Bar
{
public ContactInfo[] Contacts;
}
其中ContactInfo是......
class ContactInfo
{
public string TelephoneNumber;
}
若然,怎么样?
答案 0 :(得分:0)
不完全是。您可以使用电话号码字符串[]映射Foo:
public class Foo
{
public string[] TelephoneNumbers { get; set; }
}
Mapper.CreateMap<Foo, Bar>()
.ForMember(dest => dest.Contacts, opt => opt.MapFrom(src => src.TelephoneNumbers));
Mapper.CreateMap<string, ContactInfo>()
.ConvertUsing(value => new ContactInfo { TelephoneNumber = value });
然后你可以像这样映射:
Bar bar = Mapper.Map<Foo, Bar>(foo);