自动映射器 5.0.0 缺少源值(自定义转换器)
本文关键字:自定义 转换器 映射 | 更新日期: 2023-09-27 17:55:08
将自动映射器版本从 4.2.1 更新到 5.0.0 后,我收到编译错误,指出缺少源值。这是我的例子
public class DraftLayoutCellPropertiesConverter : ITypeConverter<DraftLayoutCell, DraftGamePeriodDraftLayoutViewModel>
{
public DraftGamePeriodDraftLayoutViewModel Convert(ResolutionContext context)
{
var input = context.SourceValue as DraftLayoutCell;
var result = new DraftGamePeriodDraftLayoutViewModel();
if (input != null)
{
该财产应该如何更换?这是进行自定义转换器的最佳方法吗?我预计更新不会破坏现有代码,因为有很多人使用该应用程序。
在自动映射器 5 中,界面ITypeConverter
更改,您需要更新您的实现:
public class DraftLayoutCellPropertiesConverter : ITypeConverter<DraftLayoutCell, DraftGamePeriodDraftLayoutViewModel>
{
public DraftGamePeriodDraftLayoutViewModel Convert(DraftLayoutCell source, DraftGamePeriodDraftLayoutViewModel destination, ResolutionContext context)
{
var input = source;
...
}
}
如我所见,ITypeConverter
有以下声明:
public interface ITypeConverter<in TSource, out TDestination>
{
TDestination Convert(TSource source, ResolutionContext context);
}
看起来你错了实现这个接口。
正确实现后,您可以使用TSource source
参数来访问您的SourceValue
。
关于您的问题"这是: if you need to use the custom
转换器进行自定义转换器的最佳方法",那么您肯定需要为其实现上面的接口。但是,这取决于您的情况,有时您可能需要使用自定义值提供程序,它可以像转换器一样使用。