通过 ForMember 从自定义地图中提取 AutoMapper 源名称
我已使用以下代码成功从现有 Automapper TypeMap 中提取了简单的源/目标对:
private MemberInfo getSource(Type destinationType, string destinationPropertyname)
{
TypeMap map = Mapper.GetAllTypeMaps()
.Where(m => m.DestinationType.Equals(destinationType))
.First();
IEnumerable<PropertyMap> properties =
map.GetPropertyMaps()
.Where(p => p.DestinationProperty
.Name
.Equals(destinationPropertyname,
StringComparison.CurrentCultureIgnoreCase));
PropertyMap sourceProperty = properties.First();
IMemberGetter mg = sourceProperty.GetSourceValueResolvers()
.Cast<IMemberGetter>()
.First();
return mg.MemberInfo;
}
但是,当我添加如下所示的自定义列映射时:
Mapper.CreateMap<Customer, CustomerViewModel>()
.ForMember(dest => dest.Cell, opt => opt.MapFrom(src => src.CellPhone))
.ForMember(dest => dest.Email, opt => opt.MapFrom(src => src.EmailAddress));
...映射的源部分无法从 GetSourceResolvers()我可以告诉。
我很感激你的任何指导。
谢谢。
-杰西·霍尔
I have successfully extracted simple source/destination pairs from an existing Automapper TypeMap using this code:
private MemberInfo getSource(Type destinationType, string destinationPropertyname)
{
TypeMap map = Mapper.GetAllTypeMaps()
.Where(m => m.DestinationType.Equals(destinationType))
.First();
IEnumerable<PropertyMap> properties =
map.GetPropertyMaps()
.Where(p => p.DestinationProperty
.Name
.Equals(destinationPropertyname,
StringComparison.CurrentCultureIgnoreCase));
PropertyMap sourceProperty = properties.First();
IMemberGetter mg = sourceProperty.GetSourceValueResolvers()
.Cast<IMemberGetter>()
.First();
return mg.MemberInfo;
}
However, when I add custom column mappings like this:
Mapper.CreateMap<Customer, CustomerViewModel>()
.ForMember(dest => dest.Cell, opt => opt.MapFrom(src => src.CellPhone))
.ForMember(dest => dest.Email, opt => opt.MapFrom(src => src.EmailAddress));
... the source part of the mapping is not available from GetSourceResolvers()
that I can tell.
I appreciate any guidance you have.
Thank you.
-Jessy Houle
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这符合你想要的吗?
Is this along the lines of what you wanted?