AutoMapper不使用派生类的集合

时间:2012-09-18 18:57:36

标签: c# automapper automapper-2

在处理包含派生类集合的对象时,我遇到了AutoMapper 2.1.267.0的问题。 我在一个更简单的场景中使用以下类来隔离我的问题:

public class ClassABase
{
    public string Name { get; set; }
}

public class ClassA : ClassABase
{
    public string Description { get; set; }
}

public class ClassBBase
{
    public string Title { get; set; }
}

public class ClassB : ClassBBase
{
    public string Text { get; set; }
}

public class ContainerA
{
    public IList<ClassA> ClassAList { get; set; }
    public ClassA ClassA { get; set; }
}

public class ContainerB
{
    public IList<ClassB> ClassBList { get; set; }
    public ClassB ClassB { get; set; }
}

和这些映射

public class ClassABaseToClassBBase : Profile
{
    protected override void Configure()
    {
        CreateMap<ClassABase, ClassBBase>()
            .Include<ClassA, ClassB>()
            .ForMember(dest => dest.Title, opt => opt.MapFrom(src => src.Name));

        Mapper.AssertConfigurationIsValid();
    }
}

public class ClassAToClassB : Profile
{
    protected override void Configure()
    {
        CreateMap<ClassA, ClassB>()
            .ForMember(dest => dest.Text, opt => opt.MapFrom(src => src.Description));

        Mapper.AssertConfigurationIsValid();
    }
}

public class ContainerAToContainerB : Profile
{
    protected override void Configure()
    {
        CreateMap<ContainerA, ContainerB>()
            .ForMember(dest => dest.ClassBList,
                       opt => opt.MapFrom(src => Mapper.Map<IList<ClassA>, IList<ClassB>>(src.ClassAList)))
            .ForMember(dest => dest.ClassB, opt => opt.MapFrom(src => src.ClassA));

    }
}

这是初始化

Mapper.Initialize(x =>
    {
        x.AddProfile<ClassABaseToClassBBase>();
        x.AddProfile<ClassAToClassB>();
        x.AddProfile<ContainerAToContainerB>();
    });

和总结一切的图表(红色箭头代表AutoMapper映射)

enter image description here

我已将ContainerA实例映射到ContainerB。第一种情况正常。目标对象已完全填充,如下图所示:

enter image description here


但是如果我添加这个映射

public class ClassBBaseToClassB : Profile
{
    protected override void Configure()
    {
        CreateMap<ClassBBase, ClassB>()
            .ForMember(dest => dest.Text, opt => opt.Ignore());

        Mapper.AssertConfigurationIsValid();
    }
}

Mapper.Initialize(x =>
    {
        x.AddProfile<ClassABaseToClassBBase>();
        x.AddProfile<ClassAToClassB>();
        x.AddProfile<ClassBBaseToClassB>();
        x.AddProfile<ContainerAToContainerB>();
    });

enter image description here

结果是

enter image description here

集合中所有元素的“Text”属性变为null。正如您在图像中看到的那样,仍然可以正确映射containerB.ClassB对象的“Text”属性。这只发生在集合中。我不知道发生了什么。有线索吗?

由于

1 个答案:

答案 0 :(得分:1)

您好,

尝试以这种方式更改容器之间的映射:

  public class ContainerAToContainerB : Profile
    {
        protected override void Configure()
        {
            CreateMap<ContainerA, ContainerB>()
                .ForMember(dest => dest.ClassBList,opt=>opt.MapFrom(src=>src.ClassAList))
                .ForMember(dest => dest.ClassB, opt => opt.MapFrom(src => src.ClassA));

        }
}