2016-07-05 17 views
4

將automapper版本從4.2.1更新到5.0.0後,發生SourceValue缺失的compilateion錯誤。 這是我的例子Automapper 5.0.0缺少SourceValue(自定義轉換器)

public class DraftLayoutCellPropertiesConverter : ITypeConverter<DraftLayoutCell, DraftGamePeriodDraftLayoutViewModel> 
    { 
     public DraftGamePeriodDraftLayoutViewModel Convert(ResolutionContext context) 
     { 
      var input = context.SourceValue as DraftLayoutCell; 
      var result = new DraftGamePeriodDraftLayoutViewModel(); 

      if (input != null) 
      { 

什麼應該是該屬性的替代?這是做自定義轉換器的最佳方式嗎?我期待的更新不會破壞現有的代碼,因爲有很多人使用該應用程序。

回答

-1

正如我所看到的,ITypeConverter有如下聲明:

public interface ITypeConverter<in TSource, out TDestination> 
{ 
    TDestination Convert(TSource source, ResolutionContext context); 
} 

它看起來像你已經錯了實現此接口。您可以使用TSource source參數訪問SourceValue

關於你的問題'是做自定義轉換器的最好方法嗎?那麼你肯定需要爲它實現上面的接口。然而,這取決於你的情況,有時你可能需要使用可以像轉換器一樣使用的自定義值提供程序。

2

在Automapper 5,接口ITypeConverter改變,你需要更新你的實現:

public class DraftLayoutCellPropertiesConverter : ITypeConverter<DraftLayoutCell, DraftGamePeriodDraftLayoutViewModel> 
{ 
    public DraftGamePeriodDraftLayoutViewModel Convert(DraftLayoutCell source, DraftGamePeriodDraftLayoutViewModel destination, ResolutionContext context) 
    { 
     var input = source; 
     ... 
    } 
}