2011-11-17 37 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; 
    }