[英]Automapper does not map base
嗨,我在使用automapper进行映射工作时遇到了一些麻烦。
我有2个DTOs BaseDto和BaseOrganizationDto
public class BaseDto
{}
public class SensitiveBaseDto : BaseDto
{}
我使用以下映射:
CreateMap<IEntity, BaseDto>()
.Include<IEntity, SensitiveBaseDto>()
.IncludeBase<IEntity, BaseDto>();
我尝试根据某些逻辑来得到某个dto
public BaseDto MapToDto(Guid asSeenById, IEntity entity)
if (entity.Id != asSeenById)
{
return this.MapToDto<BaseDto>(entity);
}
return this.MapToDto<SensitiveBaseDto>(entity);
}
但它总是返回一个SensitiveBaseDto,我已经验证了MapToDto方法中的逻辑是否正确执行。
我错过了什么?
解决这个问题:
public override TDtoType MapToDto<TDtoType>(IEntity entity)
{
var dto = typeof(TDtoType) == typeof(SensitiveDto)
? new SensitiveDto()
: new BaseDto();
this.Engine.Map(entity, dto);
return dto as TDtoType;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.