开发者

Automapper: How to get source property name from PropertyMap

开发者 https://www.devze.com 2023-04-08 19:18 出处:网络
Ho开发者_如何学JAVAw do I get the name of the source property from the property map in this code:

Ho开发者_如何学JAVAw 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)
{
    ////.....
}


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消