AutoMapper IValueResolver抛出错误该类型不能用作类型参数' TValueResolver'在泛型类型或方法中

时间:2017-01-05 22:55:59

标签: c# automapper automapper-5

我尝试在新版本的AutoMapper中实现IValueResolver接口。 我已经实现了ResourceTypeResolver类,如下所示..

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配置中使用此ResourceTypeResolver后

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

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

  

错误19类型&#39; PMEasy.Web.AutoMapper.ResourceTypeResolver&#39;不能用作类型参数&#39; TValueResolver&#39;在泛型类型或方法中&#39; AutoMapper.IMemberConfigurationExpression.ResolveUsing()&#39;。来自&#39; PMEasy.Web.AutoMapper.ResourceTypeResolver&#39;没有隐式参考转换。到AutoMapper.IValueResolver&#39;。 D:\ PMEasy \ Development \ PMEasyDev \ PMEasy.Web \ App_Start \ AutoMapperConfig.cs 58 59 PMEasy.Web

任何想法我们如何解决这个问题?

1 个答案:

答案 0 :(得分:3)

看起来,您创建了自己的界面IValueResolver<in TSource, in TDestination, TDestMember>,使用了自动播放器的界面。 Soo,删除你的界面定义。