【问题标题】:ITypeConverter interface has been changed in AutoMapper 2.0AutoMapper 2.0 中的 ITypeConverter 接口已更改
【发布时间】:2011-12-31 09:59:33
【问题描述】:

ITypeConverter 接口已更改为具有用于 Convert 方法的“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;
        }
    }

更新

ITypeConverter 接口已更改为具有用于 Convert 方法的“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

【问题讨论】:

    标签: versioning automapper


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-08-19
      • 1970-01-01
      • 1970-01-01
      • 2014-12-17
      • 1970-01-01
      相关资源
      最近更新 更多