本文介绍了自动映射器:如何从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获取源属性名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!