ITypeConverter interface has been changed in AutoM

2019-03-25 03:36发布

问题:

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:

'BusinessFacade.Mappers.DecimalToNullableInt' does not implement interface member 'AutoMapper.ITypeConverter.Convert(AutoMapper.ResolutionContext)'

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

My mapper

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

UPDATE

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

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-type-converters

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

回答1:

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;
    }