2016-10-11 26 views
1

私はAutoMapper(v5.1.1)を使用して、リストまたはコレクションから継承するオブジェクトをマップしようとしています。マップコールは私にエラーを与えませんが、出力は空のリストです(正しいタイプのものです)。オートコレクションとコレクションまたはコレクションからの継承

私はList<DestinationObject>またはCollection<DestinationObject>を得ることができますが、List<T>またはCollection<T>からenheritsカスタムクラスを持つとき、動作するようには思えません。

私は、基本クラス(List<T>)を含むように最初のマップ定義を拡張しようとしましたが、それは私にStackOverflowExceptionを与えます。

cfg.CreateMap(typeof(SourceCollection), typeof(DestinationCollection)).Include(typeof(List<SourceObject>), typeof(List<DestinationObject>)); 

私はここで何が欠けていますか?あなただけdestinationobjectするsourceobjectをマッピングする必要が

public class SourceCollection : List<SourceObject> { 

} 

public class DestinationCollection : List<DestinationObject> { 

} 

public class SourceObject { 

    public string Message { get; set; } 
} 

public class DestinationObject { 

    public string Message { get; set; } 
} 


static void Main(string[] args) 
{ 

    AutoMapper.Mapper.Initialize(cfg => 
    { 
     cfg.CreateMap(typeof(SourceCollection), typeof(DestinationCollection)); 
     cfg.CreateMap<List<SourceObject>, List<DestinationObject>>().Include<SourceCollection, DestinationCollection>(); 
     cfg.CreateMap(typeof(SourceObject), typeof(DestinationObject)); 
    }); 

    AutoMapper.Mapper.AssertConfigurationIsValid(); 

    SourceCollection srcCol = new SourceCollection() { new SourceObject() { Message = "1" }, new SourceObject() { Message = "2" } }; 
    DestinationCollection dstCol = AutoMapper.Mapper.Map<SourceCollection, DestinationCollection>(srcCol); 
} 

答えて

1

、AutoMapperは魔法の残りを行いますが、これについてはこのlink

cfg.CreateMap(typeof(SourceObject), typeof(DestinationObject)); 
+0

うわーで見つけることができ、感謝 - それだけで一つのラインを試したことがありません。 ) 面白い他の行にはAutoMapperが混乱しています。私は継承を使用していて、AutoMapperのドキュメントにはそれだけの章があります。 ありがとう! // Mike – Mike

関連する問題