コード例 #1
0
 private void MappingMembers(ConventionContext context, MappingMember[] targetMembers,
                             MappingMember[] sourceMembers, bool hierarchy)
 {
     if (hierarchy)
     {
         var minLength = Math.Min(targetMembers.Length, sourceMembers.Length);
         for (int i = 0; i < minLength; i++)
         {
             context.Mappings.Set(sourceMembers[sourceMembers.Length - 1 - i],
                                  targetMembers[targetMembers.Length - 1 - i]);
         }
     }
     else
     {
         for (int targetIndex = targetMembers.Length - 1, sourceIndex = sourceMembers.Length - 1;
              targetIndex >= 0 && sourceIndex >= 0; targetIndex--)
         {
             MappingMember targetMember = targetMembers[targetIndex], sourceMember = sourceMembers[sourceIndex];
             var           assignable = targetMember.MemberType.GetTypeInfo().IsAssignableFrom(sourceMember.MemberType);
             if (assignable || context.Converters.Get(sourceMember.MemberType, targetMember.MemberType) != null)
             {
                 context.Mappings.Set(sourceMember, targetMember);
                 sourceIndex--;
             }
             else if (targetIndex >= sourceIndex)
             {
                 sourceIndex--;
             }
         }
     }
 }
コード例 #2
0
 internal void Apply(ConventionContext context)
 {
     foreach (var convention in _conventions)
     {
         convention.Apply(context);
     }
 }
コード例 #3
0
        /// <summary>
        /// Applies the convention to type member mappings.
        /// </summary>
        /// <param name="context">The context to apply conventions.</param>
        public void Apply(ConventionContext context)
        {
            var  options          = context.Options == MemberMapOptions.Default ? Options : context.Options;
            bool includeNonPublic = HasOption(options, MemberMapOptions.NonPublic);
            var  targetMembers    = context.TargetMembers.Where(member => member.CanWrite(includeNonPublic)).ToArray();
            var  sourceMembers    = context.SourceMembers.Where(member => member.CanRead(includeNonPublic)).ToArray();
            var  comparer         = HasOption(options, MemberMapOptions.IgnoreCase)
                ? StringComparer.CurrentCultureIgnoreCase
                : StringComparer.CurrentCulture;
            var hierarchy = HasOption(options, MemberMapOptions.Hierarchy);

            foreach (var memberName in targetMembers.Select(member => member.MemberName).Distinct(comparer))
            {
                MappingMembers(context,
                               targetMembers.Where(member => comparer.Equals(member.MemberName, memberName)).ToArray(),
                               sourceMembers.Where(member => comparer.Equals(member.MemberName, memberName)).ToArray(),
                               hierarchy);
            }
        }
コード例 #4
0
 public void Apply(ConventionContext context)
 {
     _action?.Invoke(context);
 }