C# 带旋转变压器的自动映射器;错误映射类型“;
我们在另一个类中有一个类作为属性,需要使用Automapper进行映射。我们已经编写了一个解析器,它将源类属性映射到destinationMember属性。我写了下面的逻辑,但它不起作用 我们收到以下错误 映射类型时出错 映射类型:SubscriberDTO->Subscriber ConsoleAutomapperTestHarness.SubscriberDTO-> ConsoleAutomapperTestHarness.Subscriber 类型映射配置:SubscriberDTO->Subscriber ConsoleAutomapperTestHarness.SubscriberDTO-> ConsoleAutomapperTestHarness.Subscriber 属性:SubscriberSettingsC# 带旋转变压器的自动映射器;错误映射类型“;,c#,automapper,automapper-5,C#,Automapper,Automapper 5,我们在另一个类中有一个类作为属性,需要使用Automapper进行映射。我们已经编写了一个解析器,它将源类属性映射到destinationMember属性。我写了下面的逻辑,但它不起作用 我们收到以下错误 映射类型时出错 映射类型:SubscriberDTO->Subscriber ConsoleAutomapperTestHarness.SubscriberDTO-> ConsoleAutomapperTestHarness.Subscriber 类型映射配置:SubscriberDTO->S
使用AutoMapper//5.1.1.0
使用制度;
命名空间ConsoleUpperTestHarness
{
公共课程
{
公共静态void Main(字符串[]args)
{
SubscriberDTO subDTO=新的SubscriberDTO();
subDTO.allowadpfa=真;
subDTO.AllowAutoPay=true;
subDTO.SubscriberID=10000;
subDTO.FirstName=“Kishor”;
新SubscriberAutoMapper();
订户sub=Mapper.Map(subDTO);
Console.WriteLine(sub.SubscriberSettings.allowadpfa.ToString());
Console.ReadLine();
}
}
公共类订阅自动映射器
{
公共订阅自动映射器()
{
Mapper.Initialize(cfg=>{
cfg.CreateMap()
.FormMember(dest=>dest.SubscriberSettings,opt=>opt.resolvesusing());
});
assertConfigurationsValid();
}
}
公共类SubscriberAutoMapperResolver:IValueResolver
{
公共设置解析(订阅源、订阅目标、设置目标成员、解析上下文)
{
//正在工作的线路。
返回新设置(){allowadpfa=source.allowadpfa};
//不工作的线路
//var result=context.Mapper.Map(源);
//var result=Mapper.Map(源);
//var result=Mapper.Map(源,目标成员);
//var result=context.Mapper.Map(源、目标成员、上下文);
//返回结果;
}
}
公共类订户
{
公共int SubscriberID{get;set;}
公共设置订阅设置{get;set;}
公共字符串名{get;set;}
}
公共类设置
{
公共bool allowrollment{get;set;}
公共布尔允许自动支付{get;set;}
公共bool allowadpfa{get;set;}
}
公共类订阅
{
公共int SubscriberID{get;set;}
公共字符串名{get;set;}
公共bool allowrollment{get;set;}
公共布尔允许自动支付{get;set;}
公共bool allowadpfa{get;set;}
}
}
老实说,ValueResolver似乎有些过分,您可以完全删除它,只需这么少的操作就可以实现所需的结果(因为默认的AutoMapper行为使得在属性具有相同名称时显式指定属性是多余的,这与您的大多数模型基本相同):
Mapper.Initialize(cfg=>{
cfg.CreateMap()
.ForMember(d=>d.SubscriberSettings,o=>o.MapFrom(s=>s));
CreateMap();
});
您有什么问题吗?错误消息似乎解释了问题所在。我需要您的帮助来解决问题。我已经在代码中提到了我尝试过的东西,但似乎都不起作用。
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; }
}
}
Mapper.Initialize(cfg => {
cfg.CreateMap<SubscriberDTO, Subscriber>()
.ForMember(d => d.SubscriberSettings, o => o.MapFrom(s => s));
cfg.CreateMap<SubscriberDTO, Settings>();
});