AutoMapper IValueResolver 抛出错误 The type cannot be used as type parameter 'TValueResolver' in the generic type or method

AutoMapper IValueResolver throws error The type cannot be used as type parameter 'TValueResolver' in the generic type or method

我尝试在新版AutoMapper中实现IValueResolver接口。 我已经实现了 ResourceTypeResolver class 如下..

public interface IValueResolver<in TSource, in TDestination, TDestMember>
{
    TDestMember Resolve(TSource source, TDestination destination, TDestMember destMember, ResolutionContext context);
}

public class ResourceTypeResolver : IValueResolver<PMEasy.Model.Entity.Resource, PMEasy.Web.Models.Gantt.GanttResource, string>
{
    private ILookupService LookupService;
    public ResourceTypeResolver(ILookupService lookupService)
    {
        LookupService = lookupService;
    }
    public string Resolve(PMEasy.Model.Entity.Resource source, PMEasy.Web.Models.Gantt.GanttResource destination, string member, ResolutionContext context)
    {
        if (source.Type.HasValue && source.Type != Guid.Empty)
        {
            var resourceType = LookupService.GetLookupListByName("ResourceType").Where(con => con.GUID == source.Type.Value).FirstOrDefault();
            return resourceType != null ? resourceType.Value : string.Empty;
        }
        else return "Work";
    }
}

我在 Mapper Configuration 中使用这个 ResourceTypeResolver 之后

var config = new MapperConfiguration(cfg =>
        {
        cfg.CreateMap<Resource, GanttResource>()
               .ForMember(dst => dst.TypeName, opt => opt.ResolveUsing<ResourceTypeResolver>()));
        } 

我在 ResolveUsing 上下文中遇到错误,

Error 19 The type 'PMEasy.Web.AutoMapper.ResourceTypeResolver' cannot be used as type parameter 'TValueResolver' in the generic type or method 'AutoMapper.IMemberConfigurationExpression.ResolveUsing()'. There is no implicit reference conversion from 'PMEasy.Web.AutoMapper.ResourceTypeResolver' to 'AutoMapper.IValueResolver'. D:\PMEasy\Development\PMEasyDev\PMEasy.Web\App_Start\AutoMapperConfig.cs 58 59 PMEasy.Web

有什么办法可以解决这个问题吗?

看来,您创建了自己的界面 IValueResolver<in TSource, in TDestination, TDestMember>,而不是使用自动映射器。要,删除您的接口定义。