自动映射器:如何从PropertyMap获取源属性名称

编程入门 行业动态 更新时间:2024-10-27 00:29:12
本文介绍了自动映射器:如何从PropertyMap获取源属性名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

如何通过此代码从属性映射中获取源属性的名称:

How do I get the name of the source property from the property map in this code:

IEnumerable<PropertyMap> propertyMapList = Mapper.FindTypeMapFor<TFrom, TTo>().GetPropertyMaps(); foreach (PropertyMap propertyMap in propertyMapList) { ////..... }

推荐答案

这应该在AutoMapper v1中有效(尚未在v2中尝试过).

This should work in AutoMapper v1 (haven't tried in v2 yet).

foreach (PropertyMap propertyMap in propertyMapList) { var resolver = propertyMap.GetSourceValueResolvers().First(); var getter = (IMemberGetter) resolver; var info = getter.MemberInfo; }

这假定它只是从一个属性到另一个属性的沼泽标准地图,否则它将无法正常工作.因此,显然,您将需要在演员表等周围添加错误检查.

This assumes that it's just a bog-standard map from one property to another, it won't work otherwise. So, obviously, you'll want to add error checking around the cast, etc.

更多推荐

自动映射器:如何从PropertyMap获取源属性名称

本文发布于:2023-11-12 11:35:56,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1581391.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:属性   名称   映射器   PropertyMap

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!