2011-11-17 66 views
13

對於Convert方法,ITypeConverter接口已更改爲具有「TDestination Convert(ResolutionContext上下文)」而不是「TDestination Convert(TSource source)」。ITypeConverter接口已在AutoMapper 2.0中更改

http://automapper.codeplex.com/wikipage?title=Custom%20Type%20Converters

在我的代碼,現在我得到這個錯誤:

'BusinessFacade.Mappers.DecimalToNullableInt' does not implement interface member 'AutoMapper.ITypeConverter.Convert(AutoMapper.ResolutionContext)'

像我的地圖製作新的映射任何好的全樣本?我不想改變我的項目的任何代碼(或最少的代碼)...

我的映射器

public class DecimalToNullableInt : ITypeConverter<decimal, int?> 
    { 
     public int? Convert(decimal source) 
     { 
      if (source == 0) 
       return null; 
      return (int)source; 
     } 
    } 

UPDATE

的ITypeConverter界面已改爲有「TDestination轉換(ResolutionContext上下文)「而不是」TDestination Convert(TSource source)「。

該文檔剛過期。有一個ITypeConverter,如 以及TypeConverter便利類的基礎。 TypeConverter隱藏了 ResolutionContext,而ITypeConverter公開了它。

http://automapper.codeplex.com/wikipage?title=Custom%20Type%20Converters

https://github.com/AutoMapper/AutoMapper/wiki/Custom-type-converters

http://groups.google.com/group/automapper-users/browse_thread/thread/6c523b95932f4747

回答

15

你必須從ResolutionContext.SourceValue財產搶十進制:

public int? Convert(ResolutionContext context) 
    { 
     var d = (decimal)context.SourceValue; 
     if (d == 0) 
     { 
      return null; 
     } 
     return (int) d; 
    }