예제 #1
0
 internal void Apply(ConventionContext context)
 {
     foreach (var convention in _conventions)
     {
         convention.Apply(context);
     }
 }
예제 #2
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];
#if NetCore
                    var assignable = targetMember.MemberType.GetTypeInfo().IsAssignableFrom(sourceMember.MemberType);
#else
                    var assignable = targetMember.MemberType.IsAssignableFrom(sourceMember.MemberType);
#endif
                    if (assignable || context.Converters.Get(sourceMember.MemberType, targetMember.MemberType) != null)
                    {
                        context.Mappings.Set(sourceMember, targetMember);
                        sourceIndex--;
                    }
                    else if (targetIndex >= sourceIndex)
                    {
                        sourceIndex--;
                    }
                }
            }
        }
예제 #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
 private void Initialize()
 {
     if (!_initialized)
     {
         lock (_lockObj)
         {
             if (!_initialized)
             {
                 var context = new ConventionContext(_container, typeof(TSource), typeof(TTarget), _options);
                 _container.Conventions.Apply(context);
                 _targetMembers = context.TargetMembers;
                 _memberMappers = new MemberMapperCollection(_container, _options);
                 foreach (var mapping in context.Mappings)
                 {
                     _memberMappers.Set(mapping.TargetMember, mapping.SourceMember, mapping.Converter);
                 }
                 _creator = context.Creator != null
                     ? (IInstanceCreator <TTarget>) new ConventionCreator <TTarget>(context.Creator)
                     : new DefaultCreator <TTarget>();
                 _initialized = true;
             }
         }
     }
 }
예제 #5
0
 public void Apply(ConventionContext context)
 {
     _action?.Invoke(context);
 }