1

我正在尝试将一些我没有编写的代码从 AutoMapper 4.0.4 升级到 7.0.1,但我遇到了问题。有一个 TypeConverter,如下所示:

public class BaseListTypeConverter<TCol1, TCol2, T1, T2> : ITypeConverter<TCol1, TCol2>
    where TCol1 : ICollection<T1>
    where TCol2 : ICollection<T2>
    where T1 : class
    where T2 : class
{
    public TCol2 Convert(ResolutionContext context)
    {
        var sourceList = (TCol1)context.SourceValue;
        TCol2 destinationList = default(TCol2);

        if (context.PropertyMap == null
            || context.Parent == null
            || context.Parent.DestinationValue == null)
            destinationList = (TCol2)context.DestinationValue;
        else
            destinationList = (TCol2)context.PropertyMap.DestinationProperty.GetValue(context.Parent.DestinationValue);
...

但是 ITypeConverter 和 ResolutionContext 接口现在已经改变了。ResolutionContext 不再具有SourceValueDestinationValuePropertyMapParent属性。我认为由于该Covert方法的新签名具有源对象和目标对象的参数,因此我可以省略第一条if语句,如下所示:

public class BaseListTypeConverter<TCol1, TCol2, T1, T2> : ITypeConverter<TCol1, TCol2>
    where TCol1 : ICollection<T1>
    where TCol2 : ICollection<T2>
    where T1 : class
    where T2 : class
{
    public TCol2 Convert(TCol1 sourceList, TCol2 destinationList, ResolutionContext context)
    {
...

但是参数destinationList以 null 的形式出现,所以显然我仍然需要该if语句正在执行的任何逻辑,但是如何为 AutoMapper 7 重写它?

4

0 回答 0