映射的代码和第3节类似:
/// <summary>更复杂对象的映射例子</summary> static void Test3() { TinyMapper.Bind<Person, PersonDto>(config => { config.Ignore(x => x.Id);//忽略ID字段 //将源类型和目标类型的字段对应绑定起来 config.Bind(x => x.Name, y => y.UserName); config.Bind(x => x.Age, y => y.Age); config.Bind(x => x.Address, y => y.Address); config.Bind(x => x.Emails, y => y.Emails); }); var person = new Person { Id = Guid.NewGuid().ToString(), Name = "John", Age = 22, Address = new Address() { Phone = "1880393", Street = "Shanghai", ZipCode = "121212" }, Emails = new List<string>() { "aaa@bb.com", "acx@cc.com" } }; var personDto = TinyMapper.Map<PersonDto>(person); }效果如下:
Tiny Mapper官方网站:
github项目地址:https://github.com/TinyMapper/TinyMapper
本文使用的代码下载:TinyMapperDemo.rar