Automapper: How to get source property name from PropertyMap

China☆狼群 提交于 2019-12-11 00:37:48

问题


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)
{
    ////.....
}

回答1:


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.



来源:https://stackoverflow.com/questions/7561602/automapper-how-to-get-source-property-name-from-propertymap

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!