[英]Map a method to a field with Automapper
我想使用 Automapper 并且需要将方法映射到字段(例如命令部分)。 我该怎么做? 这是我的第一个代码
foreach(source item in sources)
{
ss.ServerStatusType = ServerStatusTypeName(item);
ss.A = item.A ;
ss.B =item.B;
destinations.Add(dto);
}
我想使用 Automapper,但我对 ServerStatusTypeName 方法有问题
var config = new MapperConfiguration(cfg =>
cfg.CreateMap<IList<source >, IList<Destination>>());
IList<Source> statuses = context.Sp_ServerUpdateStatus(userName).ToList();
var mapper = new Mapper(config);
IList<Source> dto = mapper.Map<IList<Destination>>(statuses);
一切正常,只是 ss.ServerStatusType 为 null 因为我使用一种方法来填充此项目
你可以试试
class YourObjectProfile : Profile
{
public YourObjectProfile()
{
CreateMap<UI_Object, Domain_Object>()
.ForMember(c => c.name, p => p.MapFrom(dbc => dbc.name));
CreateMap<Domain_Object, UI_Object>()
.ForMember(dbc => dbc.name, dbp => dbp.MapFrom(c => c.name));
}
}
我使用此代码,没问题
CreateMap<Sp_ServerUpdateStatus_Result, ServerStatus>()
.ForMember(dest=>dest.ServerStatusType, opt => opt.MapFrom(src=> ServerStatusTypeName(src))
);
... ServerStatusTypeName(src) 是一种方法
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.