Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/blackberry/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 自动映射器不映射基_C#_Automapper - Fatal编程技术网

C# 自动映射器不映射基

C# 自动映射器不映射基,c#,automapper,C#,Automapper,嗨,我有一些麻烦,使我的映射工作在automapper 我有两个DTO BaseDto和BaseOrganizationDto public class BaseDto {} public class SensitiveBaseDto : BaseDto {} 我使用以下映射: CreateMap<IEntity, BaseDto>() .Include<IEntity, SensitiveBaseDto>()

嗨,我有一些麻烦,使我的映射工作在automapper

我有两个DTO BaseDto和BaseOrganizationDto

public class BaseDto
{}

public class SensitiveBaseDto : BaseDto
{}
我使用以下映射:

CreateMap<IEntity, BaseDto>()
                .Include<IEntity, SensitiveBaseDto>()
                .IncludeBase<IEntity, BaseDto>();
CreateMap()
.包括()
.IncludeBase();
我试着根据一些逻辑得到一个特定的dto,比如

public BaseDto MapToDto(Guid asSeenById, IEntity entity)
    if (entity.Id != asSeenById)
    {
      return this.MapToDto<BaseDto>(entity);
    }
    return this.MapToDto<SensitiveBaseDto>(entity);
}
public-BaseDto映射到(Guid-asSeenById,IEntity实体)
if(entity.Id!=asSeenById)
{
返回此.maptodo(实体);
}
返回此.maptodo(实体);
}
但是它总是返回一个SensitiveBaseDto,我已经验证了maptodo方法中的逻辑是否正确执行


我遗漏了什么?

通过这样做解决了这个问题:

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;
}
public override TDtoType maptodo(entity实体)
{
var dto=类型化(TDtoType)=类型化(敏感性)
?新的敏感度()
:new BaseDto();
此.Engine.Map(实体,dto);
将dto作为TDtoType返回;
}

AFAIK,使用
Include
设置映射时,源类和目标类层次结构必须完全匹配。更好的解决方案可能是创建自定义转换器,并在转换器中创建正确的
Dto