Automapper 不映射基础

Automapper does not map base

您好,我在使用 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);
}

但它总是 returns 一个 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;
}