Automapper: How to get source property name from P

2019-07-21 07:00发布

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
1条回答
老娘就宠你
2楼-- · 2019-07-21 07:56

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.

查看更多
登录 后发表回答