1

我尝试在新版本的 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 Configuration 中使用了这个 ResourceTypeResolver 之后

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

在这里,我在 ResolveUsing context 中遇到错误,

错误 19 类型“PMeasy.Web.AutoMapper.ResourceTypeResolver”不能用作泛型类型或方法“AutoMapper.IMemberConfigurationExpression.ResolveUsing()”中的类型参数“TValueResolver”。没有从“PMEASy.Web.AutoMapper.ResourceTypeResolver”到“AutoMapper.IValueResolver”的隐式引用转换。D:\PMEasy\Development\PMEasyDev\PMEasy.Web\App_Start\AutoMapperConfig.cs 58 59 PMEasy.Web

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

4

1 回答 1

11

看来,您创建了自己的界面IValueResolver<in TSource, in TDestination, TDestMember>,而不是使用 automapper 的界面。所以,删除你的接口定义。

于 2017-01-05T23:12:28.157 回答