问题描述
在AutoMapper 4.1.1中,可以在条件中访问属性映射,源成员和目标成员信息,这对于解决我们仅希望映射已赋予值且已被明确映射到目标成员。我们使用以下代码片段进行了此操作:
.ForAllMembers(o => o.Condition(c =>
{
// Check if source is not mapped directly (function or expression is used instead)
if (c.PropertyMap.sourceMember == null)
return true;
// Only if external shopper's properties are set and mapped already,then map them to the local customer properties
// Mapped properties in external shopper are in MappedPropertySet
var sourcePropertyName = c.PropertyMap.sourceMember.Name;
return ((ExternalShopper)c.Parent.sourceValue).MappedPropertySet.Contains(sourcePropertyName);
}));
为了获得较新版本的解析上下文,必须将方法签名更改为:
.ForAllMembers(o => o.Condition((src,dest,srcmember,destMember,c) =>
似乎在较新版本的AutoMapper中,PropertyMap已从ResolutionContext中删除。据我所知,没有办法找出条件调用中当前正在评估哪个成员?
是否有可能具有相同的“所有成员”条件逻辑,而无需在每个.ForMember上显式设置。(Pre)Condition,也无需为要映射/控制的每个目标属性显式添加.ForMember?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)