AutoMapper:6.2.2.0
案例:UserDto和User实体之间的映射(User类除了Name拥有更多字段)
[AutoMap(typeof(User))]
public class UserDto {
public string Name { get; set; }
}
最后直接使用:var userDto = userService.Find("001").MapTo<UserDto>(); 完成映射
自定义特性:
1.映射方向,也就是Api中CreateMap 从哪映射到哪。
public enum AutoMapDirection
{
From,
To
}
2.映射自定义特性
public class AutoMapAttribute : Attribute
{
public Type[] TargetTypes { get; private set; }
internal virtual AutoMapDirection Direciton
{
get { return AutoMapDirection.From | AutoMapDirection.To; }
}
public AutoMapAttribute(params Type[] targetTypes)
{
TargetTypes =