您好,我在 automapper 中进行映射时遇到了一些问题。
我有 2 个 DTO 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;
}
关于c# - Automapper 不映射基础,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32332205/