如何将 AutoMapper 客户解析器与 .ForPath 一起使用

问题描述 投票:0回答:0

我正在将我的模型映射到信息类,其中模型的第一级属性需要映射到信息类的子级属性。

在我的模型中,我有字符串,我需要使用自定义解析器根据文本填充 Info 类的对象。 我不能使用“ForMember”和“ForPath”我得到编译时异常。

我的模特:

public class VisitorPolicyRequest : IMapFrom<VisitorPolicyInfo>
    {

        public string ProductCode { get; set; }
        public string MemberFullNameEn { get; set; }
        public string MemberEmailAddress { get; set; }
        public long MemberMobileNumber { get; set; }
        public string MemberNationality { get; set; }
        public string? PassportIssueCountry { get; set; }

        public void ReverseMapping(Profile profile)
        {
            profile.CreateMap<VisitorPolicyRequest, VisitorPolicyInfo>()
                .ForPath(s => s.Member.FullNameEn, x => x.MapFrom(o => o.MemberFullNameEn))
                .ForPath(s => s.Member.EmailAddress, x => x.MapFrom(o => o.MemberEmailAddress))
                .ForPath(s => s.Member.MobileNumber, x => x.MapFrom(o => o.MemberMobileNumber))
                .ForPath(s => s.Member.LazyCountry, x => x.MapFrom<CountryResolver, string?>(o => o.PassportIssueCountry))  
                .ForMember(s => s.InsuranceProduct, x => x.MapFrom<InsuranceProductResolver, string>(x => x.ProductCode))
                .ReverseMap();

            profile.CreateMap<VisitorPolicyRequest, ErrorEmailRequestModel>().ReverseMap();
        }
    }

这是我用来从模型中收到的缓存中读取匹配值的解析器:

public class CountryResolver : IMemberValueResolver<object, object, string?, Lazy<CountryInfo>?>
    {
        private readonly ICountryProvider _provider;

        public CountryResolver(ICountryProvider provider)
        {
            _provider = provider;
        }

        public Lazy<CountryInfo>? Resolve(object source, object destination, string? sourceMember, Lazy<CountryInfo>? destMember, ResolutionContext context)
        {
            if (sourceMember == null)
                return null;
            return new Lazy<CountryInfo>(() => _provider.GetCountryByName(sourceMember));
        }

    }

例外:

使用泛型方法 'IPathConfigurationExpression.MapFrom(Expression>)' 需要 1 个类型参数

c# automapper
© www.soinside.com 2019 - 2024. All rights reserved.