AutoMapperを使用してオブジェクトのフラット化を試みています。AutoMapper:複数のソースプロパティからのコレクションへのマップ
public class Source
{
public string Name {get;set;}
public string Child1Property1 {get;set;}
public string Child1Property2 {get;set;}
public string Child2Property1 {get;set;}
public string Child2Property2 {get;set;}
}
私は私のマッピング設定
public static class AutoMapperConfiguration
{
public static MapperConfiguration Configure()
{
var config = new MapperConfiguration(
cfg =>
{
cfg.CreateMap<Source, Destination>()
.ForMember(dest => dest.Children, /* What do I put here?*/))
// I don't think this is correct
cfg.CreateMap<Source, Child>()
.ForMember(dest => dest.Property1, opt => opt.MapFrom(src => src.Child1Property1))
.ForMember(dest => dest.Property2, opt => opt.MapFrom(src => src.Child1Property2))
.ForMember(dest => dest.Property1, opt => opt.MapFrom(src => src.Child2Property1))
.ForMember(dest => dest.Property2, opt => opt.MapFrom(src => src.Child2Property2));
});
return config;
}
}
先にこれに
public class Destination
{
public string Name {get;set;}
public List<Child> Children {get;set;}
}
public class Child
{
public string Property1 {get;set;}
public string Property2 {get;set;}
}
をマップするを次のように私は今、私は自分のコードをテストするときに私が得るソースを持っていますmapper.Map<List<Child>>(source)
を使用して私はAutoMapperMappingException: Missing type map configuration or unsupported mapping.
を得ます。これは、マッピングcがないので意味がありますList<Child>
に設定されています。 mapper.Map<Child>(source)
を実行すると、プロパティの値がすべてnull
のChild
インスタンスになります。
私は残念ながらSource
クラスを変更する立場にはありません。
AutoMapperでこれはまったく可能ですか?もしそうなら、どのように?
簡単な例を示してください – liorsolomon