在AutoMapper 2.0中更改了ITypeConverter接口

时间:2011-11-17 14:34:39

标签: versioning automapper

对于Convert方法,ITypeConverter接口已更改为“TDestination Convert(ResolutionContext context)”而不是“TDestination Convert(TSource source)”。

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

在我的代码中,现在我收到了这个错误:

  

'BusinessFacade.Mappers.DecimalToNullableInt'未实现   接口成员   'AutoMapper.ITypeConverter.Convert(AutoMapper.ResolutionContext)'

像我的地图选手一样,新地图的完整样本是什么?我不希望在我的项目中更改任何代码(或最小代码)......

我的映射器

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

更新

对于Convert方法,ITypeConverter接口已更改为“TDestination Convert(ResolutionContext context)”而不是“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

1 个答案:

答案 0 :(得分:15)

您必须从ResolutionContext.SourceValue属性中获取小数:

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