2012-09-18 75 views
5

我在處理包含派生類集合的對象時遇到了AutoMapper 2.1.267.0的問題。 我孤立我的問題在一個簡單的方案有以下類別:AutoMapper不適用於派生類的集合

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

集合中所有元素的「文本」屬性都爲空。正如您在圖像中看到的那樣,containerB.ClassB對象的「Text」屬性仍然正確映射。這隻發生在集合中。我不知道發生了什麼事。任何線索?

感謝

+0

你期待什麼樣的結果? – Cybermaxs

+0

我期待containerB.ClassBList中的所有元素都被完全填充,而不是具有空屬性。 – peflorencio

回答

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)); 

     } 
} 
相關問題