2017-06-06 8 views
0

Automapper 3.3.0から6.0.2にアップグレードしたばかりで、ソリューションをアップグレードした後、マッピングの1つがもう機能していないようです。Automapper - オーバーライドされたプロパティをマッピングするとキャストが発生する例外

この考えてみましょう:私は新しい「DerivedDestinationMaster」インスタンスに「ソース」オブジェクトをマップしようとした場合、私が得る、

CreateMap<Source,DestinationMaster>() 
    .Include<DerivedSource,DerivedDestinationMaster>(); 

CreateMap<DerivedSource,DerivedDestinationMaster>() 
    .ForMember(dest => dest.Child, act=> act.MapFrom(src => new DerivedDestinationChild(){ GrossAmount = src.GrossAmount })); 

:マッピングの

public class Source{ 
    public Decimal GrossAmount {get;set;} 
} 

public class DerivedSource : Source{ 
} 

public class DestinationMaster{ 
    public virtual DestinationChild Child {get;set;} 
} 

public class DestinationChild{ 
    public decimal GrossAmount {get;set;} 
} 

public class DerivedDestinationMaster : DestinationMaster { 
    private DerivedDestinationChild _destinationChild; 

    public override DestinationChild Child { 
     get{return _destinationChild;} 
     set{_destinationChild = (DerivedDestinationChild)value;} 
    } 
} 

public class DerivedDestinationChild : DestinationChild{ 
    public string OtherProperty {get;set;} 
} 

そして今を"DestinationChild"を "DerivedDestinationChild"に変換できないという例外があります。

"setter"アクセサのオーバーライドプロパティにブレークポイントを設定すると、渡されたインスタンスのタイプが "DestinationChild"であることがわかります。 "DestinactionChild"クラスのコンストラクタコールが2つあることもわかります。 "GrossAmount"値は、 "DerivedDestinationChild"を作成してインスタンス化した後、 "DestinationChild"の新しいインスタンスを作成して情報を受け取って2つのオブジェクトをオートマッシュするかのように、0ではなく "Source"オブジェクトの良い値です。

私たちのシナリオはもう少し複雑ですが(私たちはIValueResolverを使用します)、 "MapFrom"、 "UseValue"とカスタムリゾルバでテストしました。最終的なインスタンスは派生型ではありません。

我々はマッピングに「act.Ignoreを()」を入れた場合は、例外が

を提起されていないがあなたの助けをありがとう!

答えて

0

文書hereとして、マッピングは継承ツリーの特定の順序に従います。あなたがそれを働かせる方法は、いくつかの地図を複製することです、それは実行可能なソリューションですか?それ以外

class Program 
{ 
    static void Main(string[] args) 
    { 
     Mapper.Initialize(cfg => 
     { 
      cfg.CreateMap<Source, DestinationMaster>().ForMember(dest => dest.Child, act => act.MapFrom(s => new DestinationChild { GrossAmount = s.GrossAmount})); 
      cfg.CreateMap<DerivedSource, DerivedDestinationMaster>().IncludeBase<Source, DestinationMaster>().ForMember(dest => dest.Child, act => act.MapFrom(s => new DerivedDestinationChild { GrossAmount = s.GrossAmount })); 
     }); 

     var source = new Source() {GrossAmount = 99}; 
     var destinationMaster = Mapper.Map<DestinationMaster>(source); 
     Debug.Assert(destinationMaster.Child.GrossAmount == source.GrossAmount); 
     Debug.Assert(destinationMaster.Child.GetType() == typeof(DestinationChild)); 

     var derivedSource = new DerivedSource() {GrossAmount = 10}; 
     var derivedDestinationMaster = Mapper.Map<DerivedDestinationMaster>(derivedSource); 
     Debug.Assert(derivedDestinationMaster.Child.GrossAmount == derivedSource.GrossAmount); 
     Debug.Assert(derivedDestinationMaster.Child.GetType() == typeof(DerivedDestinationChild)); 
    } 
} 

私はあなたが基本マッピングからマッピングされたGrossAmountで特定の派生型を返すことができるかわかりません。

関連する問題