Automapper 5.0.0缺少SourceValue(自定义转换器)

时间:2016-07-05 13:08:17

标签: c# automapper

将automapper版本从4.2.1更新到5.0.0后,我得到了缺少SourceValue的编译错误。 这是我的例子

 public class DraftLayoutCellPropertiesConverter : ITypeConverter<DraftLayoutCell, DraftGamePeriodDraftLayoutViewModel>
    {
        public DraftGamePeriodDraftLayoutViewModel Convert(ResolutionContext context)
        {
            var input = context.SourceValue as DraftLayoutCell;
            var result = new DraftGamePeriodDraftLayoutViewModel();

            if (input != null)
            {

该财产的替代品应该是什么?这是定制转换器的最佳方式吗?我期待更新不会破坏现有代码,因为有很多人使用该应用程序。

2 个答案:

答案 0 :(得分:4)

在Automapper 5中,界面ITypeConverter已更改,您需要更新实施:

public class DraftLayoutCellPropertiesConverter : ITypeConverter<DraftLayoutCell, DraftGamePeriodDraftLayoutViewModel>
{
    public DraftGamePeriodDraftLayoutViewModel Convert(DraftLayoutCell source, DraftGamePeriodDraftLayoutViewModel destination, ResolutionContext context)
    {
        var input = source;
        ...
    }
}

答案 1 :(得分:-1)

我可以看到,ITypeConverter有以下声明:

public interface ITypeConverter<in TSource, out TDestination>
{
    TDestination Convert(TSource source, ResolutionContext context);
}

看起来你错误地实现了这个界面。

正确实施后,您可以使用TSource source参数访问SourceValue

关于您的问题&#39;这是自定义转换器: if you need to use the custom converter`的最佳方式,那么您肯定需要为它实现上面的接口。但是,这取决于您的情况,有时您可能需要使用自定义值提供程序,它可以像转换器一样使用。