예제 #1
0
        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);
        }
예제 #2
0
 private bool IsExpressionMapper(MemberInfo targetMember, BindingConfig bindingConfig)
 {
     if (bindingConfig != null)
     {
         if (bindingConfig.IsExpressionMapper(targetMember.Name))
         {
             return(true);
         }
     }
     return(false);
 }
예제 #3
0
 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);
 }
예제 #4
0
 public static void Add(TypePair typePair, BindingConfig bindingConfig)
 {
     Cache.Add(typePair, bindingConfig);
 }
예제 #5
0
        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());
            }
        }