2011-11-17 22 views
13

Giao diện ITypeConverter đã được thay đổi thành "TDestination Convert (ResolutionContext context)" thay vì "TDestination Convert (TSource source)" cho phương thức Convert.Giao diện ITypeConverter đã được thay đổi trong AutoMapper 2.0

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

Trong mã của tôi, bây giờ tôi nhận được lỗi này:

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

Bất kỳ tốt đầy đủ mẫu cho mapper mới như người vẽ bản đồ của tôi? Tôi không muốn thay đổi bất kỳ mã (hoặc mã tối thiểu) trong các dự án của tôi ...

mapper My

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

CẬP NHẬT

Giao diện ITypeConverter đã được thay đổi để có một "TDestination Chuyển đổi (ResolutionContext context) "thay vì" TDestination Convert (nguồn TSource) "cho phương thức Convert.

tài liệu đã lỗi thời. Có một ITypeConverter, như là cũng như một lớp tiện lợi TypeConverter cơ sở. Các TypeConverter ẩn các ResolutionContext, trong khi ITypeConverter phơi bày nó.

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

Trả lời

15

Bạn sẽ phải lấy số thập phân từ ResolutionContext.SourceValue tài sản:

public int? Convert(ResolutionContext context) 
    { 
     var d = (decimal)context.SourceValue; 
     if (d == 0) 
     { 
      return null; 
     } 
     return (int) d; 
    } 
Các vấn đề liên quan