在AutoMapper 2.0中更改了ITypeConverter接口

Kiq*_*net 13 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;
        }
    }
Run Code Online (Sandbox Code Playgroud)

UPDATE

对于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

Pat*_*ele 15

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

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