带有解析器的Automapper抛出“错误映射类型”

我们在另一个类中有一个类作为属性,需要使用Automapper进行映射.我们已经编写了一个解析器,它将源类属性映射到destinationMember属性.我写了下面的逻辑不起作用.

我们收到以下错误.

Error mapping types.

Mapping types: SubscriberDTO -> Subscriber
ConsoleAutomapperTestHarness.SubscriberDTO ->
ConsoleAutomapperTestHarness.Subscriber

Type Map configuration: SubscriberDTO -> Subscriber
ConsoleAutomapperTestHarness.SubscriberDTO ->
ConsoleAutomapperTestHarness.Subscriber

Property: SubscriberSettings

using AutoMapper; //5.1.1.0
using System;

namespace ConsoleAutomapperTestHarness
{
   public class Program
    {
        public static void Main(string[] args)
        {
            SubscriberDTO subDTO = new SubscriberDTO();
            subDTO.AllowAddPFA = true;
            subDTO.AllowAutoPay = true; ;
            subDTO.SubscriberID = 10000;
            subDTO.FirstName = "Kishor";

            new SubscriberAutoMapper();

            Subscriber sub = Mapper.Map<SubscriberDTO, Subscriber>(subDTO);
            Console.WriteLine(sub.SubscriberSettings.AllowAddPFA.ToString());
            Console.ReadLine();
        }
    }

    public class SubscriberAutoMapper
    {
        public SubscriberAutoMapper()
        {
            Mapper.Initialize(cfg => {
                cfg.CreateMap<SubscriberDTO, Subscriber>()
                .ForMember(dest => dest.SubscriberSettings, opt => opt.ResolveUsing<SubscriberAutoMapperResolver>());                
            });
            Mapper.AssertConfigurationIsValid();
        }
    }
    public class SubscriberAutoMapperResolver : IValueResolver<SubscriberDTO, Subscriber, Settings>
    {
        public Settings Resolve(SubscriberDTO source, Subscriber destination, Settings destMember, ResolutionContext context)
        {
            //line which is working.
        return new Settings() { AllowAddPFA = source.AllowAddPFA };

        //line which is not working
       // var result = context.Mapper.Map<SubscriberDTO, Settings>(source);
       // var result = Mapper.Map<SubscriberDTO, Settings>(source);
        //var result = Mapper.Map<SubscriberDTO, Settings>(source,destMember);
        //var result = context.Mapper.Map<SubscriberDTO, Settings>(source, destMember, context);
        //return result;           

        }
    }
    public class Subscriber
    {
        public int SubscriberID { get; set; }
        public Settings SubscriberSettings { get; set; }
        public string FirstName { get; set; }
    }
    public class Settings
    {
        public bool AllowEnrollment { get; set; }
        public bool AllowAutoPay { get; set; }
        public bool AllowAddPFA { get; set; }

    }

    public class SubscriberDTO
    {
        public int SubscriberID { get; set; }
        public string FirstName { get; set; }

        public bool AllowEnrollment { get; set; }
        public bool AllowAutoPay { get; set; }
        public bool AllowAddPFA { get; set; }
    }


}

解决方法:

从本质上讲,ValueResolver似乎过于矫kill过正,您可以花很少的钱就可以完全删除它并获得所需的结果(假设默认的AutoMapper行为使得当它们具有相同的名称时,显式地指定属性变得多余了,基本上就像大多数模型一样) :

Mapper.Initialize(cfg => {
    cfg.CreateMap<SubscriberDTO, Subscriber>()
        .ForMember(d => d.SubscriberSettings, o => o.MapFrom(s => s));
    cfg.CreateMap<SubscriberDTO, Settings>();   
});
上一篇:C#自动映射器将日期时间格式化为ISO字符串


下一篇:CodeGo.net> Automapper-如何从源子对象映射到目标