private List <MappingMember> ParseMappingTypes(TypePair typePair) { var result = new List <MappingMember>(); List <MemberInfo> sourceMembers = GetSourceMembers(typePair.Source); List <MemberInfo> targetMembers = GetTargetMembers(typePair.Target); BindingConfig bindingConfig = BindingConfig.Get(typePair); foreach (var member in targetMembers) { if (IsIgnore(member, bindingConfig)) { result.Add(MappingMember.Ignore(member)); continue; } if (IsExpressionMapper(member, bindingConfig)) { result.Add(MappingMember.Expreesion(member)); continue; } MemberInfo sourceMember = GetSourceName(sourceMembers, member, bindingConfig); if (sourceMember.IsNull()) { result.Add(MappingMember.Ignore(member)); continue; } result.Add(MappingMember.Mapper(member, sourceMember)); } return(result); }
private bool IsExpressionMapper(MemberInfo targetMember, BindingConfig bindingConfig) { if (bindingConfig != null) { if (bindingConfig.IsExpressionMapper(targetMember.Name)) { return(true); } } return(false); }
private bool IsIgnore(MemberInfo targetMember, BindingConfig bindingConfig) { if (bindingConfig != null) { var sourceName = bindingConfig.GetBindField(targetMember.Name); if (sourceName != null) { return(false); } if (bindingConfig.IsIgnoreTargetField(targetMember.Name)) { return(true); } } if (_config.IsGlobalIgnore(targetMember)) { return(true); } return(false); }
public static void Add(TypePair typePair, BindingConfig bindingConfig) { Cache.Add(typePair, bindingConfig); }
private MemberInfo GetSourceName(List <MemberInfo> sourceMembers, MemberInfo targetMember, BindingConfig bindingConfig) { if (bindingConfig != null) { var sourceName = bindingConfig.GetBindField(targetMember.Name); if (sourceName != null) { return(sourceMembers.FirstOrDefault(x => x.Name.Equals(sourceName))); } } var matchSourceMembers = sourceMembers.Where(x => _config.NameMatching(targetMember.Name, x.Name)); if (matchSourceMembers.Count() <= 1) { return(matchSourceMembers.FirstOrDefault()); } switch (_config.MultiMatchHandle) { case MultiMatchHandle.Ignore: return(null); case MultiMatchHandle.Error: throw new Exception($"找到多个匹配的成员。TargetMemberName:{targetMember.Name}"); default: return(matchSourceMembers.FirstOrDefault()); } }