本文介绍了ITypeConverter接口在AutoMapper 2.0中已更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

已将ITypeConverter接口更改为具有"TDestination Convert(ResolutionContext context)",而不是Convert方法的"TDestination Convert(TSource source)".

The ITypeConverter interface has been changed to have a "TDestination Convert(ResolutionContext context)" instead of "TDestination Convert(TSource source)" for the Convert method.

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

在我的代码中,现在出现此错误:

In my code, now I get this error:

像我的制图员一样,有什么好的新制图员完整样本吗?我不想更改项目中的任何代码(或最低代码)...

Any good full sample for new mapper like my mappers ? I don't want change any code (or minimum code) in my projects...

我的地图绘制者

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

更新

已将ITypeConverter接口更改为具有"TDestination Convert(ResolutionContext context)",而不是Convert方法的"TDestination Convert(TSource source)".

The ITypeConverter interface has been changed to have a "TDestination Convert(ResolutionContext context)" instead of "TDestination Convert(TSource source)" for the Convert method.

该文档已过时.有一个ITypeConverter,如以及基本的TypeConverter便利类. TypeConverter隐藏了ResolutionContext,而ITypeConverter公开它.

the documentation is just out of date. There is an ITypeConverter, as well as a base TypeConverter convenience class. The TypeConverter hides the ResolutionContext, while ITypeConverter exposes it.

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

https://github.com/AutoMapper/AutoMapper/wiki/Custom-类型转换器

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

推荐答案

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

You'll have to grab the decimal from the ResolutionContext.SourceValue property:

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

这篇关于ITypeConverter接口在AutoMapper 2.0中已更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 02:38