Exemplo n.º 1
0
        public static TDestination Map <TSource, TDestination>(TSource source, TDestination destination, ResolutionContext context, Func <TDestination> ifNull)
        {
            if (destination == null)
            {
                destination = ifNull();
            }
            object boxedDestination       = destination;
            var    destinationTypeDetails = context.ConfigurationProvider.Configuration.CreateTypeDetails(typeof(TDestination));

            foreach (var member in destinationTypeDetails.PublicWriteAccessors)
            {
                object sourceMemberValue;
                try
                {
                    sourceMemberValue = GetDynamically(member, source);
                }
                catch (RuntimeBinderException)
                {
                    continue;
                }
                var destinationMemberValue = context.MapMember(member, sourceMemberValue, boxedDestination);
                member.SetMemberValue(boxedDestination, destinationMemberValue);
            }
            return((TDestination)boxedDestination);
        }
Exemplo n.º 2
0
        private static object Map(
            object source,
            object destination,
            Type destinationType,
            ResolutionContext context,
            ProfileMap profileMap
            )
        {
            destination ??= ObjectFactory.CreateInstance(destinationType);
            var destinationTypeDetails = profileMap.CreateTypeDetails(destinationType);

            foreach (var member in destinationTypeDetails.WriteAccessors)
            {
                object sourceMemberValue;
                try
                {
                    sourceMemberValue = GetDynamically(member.Name, source);
                }
                catch (RuntimeBinderException)
                {
                    continue;
                }
                var destinationMemberValue = context.MapMember(
                    member,
                    sourceMemberValue,
                    destination
                    );
                member.SetMemberValue(destination, destinationMemberValue);
            }
            return(destination);
        }
Exemplo n.º 3
0
        public static TDestination Map <TSource, TDestination>(TSource source, TDestination destination, ResolutionContext context)
        {
            if (destination == null)
            {
                destination = (TDestination)(!context.ConfigurationProvider.AllowNullDestinationValues
                    ? ObjectCreator.CreateNonNullValue(typeof(TDestination))
                    : ObjectCreator.CreateObject(typeof(TDestination)));
            }
            var memberContext = new ResolutionContext(context);

            foreach (var member in new TypeDetails(typeof(TSource)).PublicWriteAccessors)
            {
                object sourceMemberValue;
                try
                {
                    sourceMemberValue = member.GetMemberValue(source);
                }
                catch (RuntimeBinderException)
                {
                    continue;
                }
                var destinationMemberValue = memberContext.MapMember(member, sourceMemberValue);
                SetDynamically(member, destination, destinationMemberValue);
            }
            return(destination);
        }
        private static TDestination Map <TDestination>(StringDictionary source, TDestination destination, ResolutionContext context, ProfileMap profileMap)
        {
            var destTypeDetails = profileMap.CreateTypeDetails(typeof(TDestination));
            var members         = from name in source.Keys
                                  join member in destTypeDetails.PublicWriteAccessors on name equals member.Name
                                  select member;
            object boxedDestination = destination;

            foreach (var member in members)
            {
                var value = context.MapMember(member, source[member.Name], boxedDestination);
                member.SetMemberValue(boxedDestination, value);
            }
            return((TDestination)boxedDestination);
        }
Exemplo n.º 5
0
        private static TDestination Map <TDestination>(StringDictionary source, TDestination destination, ResolutionContext context)
        {
            destination = destination == null?context.Mapper.CreateObject <TDestination>() : destination;

            var destTypeDetails = context.ConfigurationProvider.Configuration.CreateTypeDetails(typeof(TDestination));
            var members         = from name in source.Keys
                                  join member in destTypeDetails.PublicWriteAccessors on name equals member.Name
                                  select member;

            foreach (var member in members)
            {
                var value = context.MapMember(member, source[member.Name], destination);
                member.SetMemberValue(destination, value);
            }
            return(destination);
        }
Exemplo n.º 6
0
        private static TDestination Map <TDestination>(StringDictionary source, ResolutionContext context)
        {
            TDestination destination     = context.Mapper.CreateObject <TDestination>(context);
            var          destTypeDetails = new TypeDetails(context.DestinationType, _ => true, _ => true);
            var          members         = from name in source.Keys
                                           join member in destTypeDetails.PublicWriteAccessors on name equals member.Name
                                           select member;
            var memberContext = new ResolutionContext(context);

            foreach (var member in members)
            {
                object value = memberContext.MapMember(member, source[member.Name]);
                member.SetMemberValue(destination, value);
            }
            return(destination);
        }
Exemplo n.º 7
0
        public static TDestination Map <TSource, TDestination>(TSource source, TDestination destination, ResolutionContext context, ProfileMap profileMap)
        {
            var sourceTypeDetails = profileMap.CreateTypeDetails(typeof(TSource));

            foreach (var member in sourceTypeDetails.PublicReadAccessors)
            {
                object sourceMemberValue;
                try
                {
                    sourceMemberValue = member.GetMemberValue(source);
                }
                catch (RuntimeBinderException)
                {
                    continue;
                }
                var destinationMemberValue = context.MapMember(member, sourceMemberValue);
                SetDynamically(member.Name, destination, destinationMemberValue);
            }
            return(destination);
        }
Exemplo n.º 8
0
        private static TDestination Map <TSource, TDestination>(TSource source, TDestination destination, ResolutionContext context, ProfileMap profileMap)
        {
            object boxedDestination       = destination;
            var    destinationTypeDetails = profileMap.CreateTypeDetails(typeof(TDestination));

            foreach (var member in destinationTypeDetails.PublicWriteAccessors)
            {
                object sourceMemberValue;
                try
                {
                    sourceMemberValue = GetDynamically(member.Name, source);
                }
                catch (RuntimeBinderException)
                {
                    continue;
                }
                var destinationMemberValue = context.MapMember(member, sourceMemberValue, boxedDestination);
                member.SetMemberValue(boxedDestination, destinationMemberValue);
            }
            return((TDestination)boxedDestination);
        }
Exemplo n.º 9
0
 public static TDestination Map <TSource, TDestination>(TSource source, TDestination destination, ResolutionContext context, Func <TDestination> ifNull)
 {
     if (destination == null)
     {
         destination = ifNull();
     }
     foreach (var member in new TypeDetails(typeof(TSource)).PublicWriteAccessors)
     {
         object sourceMemberValue;
         try
         {
             sourceMemberValue = member.GetMemberValue(source);
         }
         catch (RuntimeBinderException)
         {
             continue;
         }
         var destinationMemberValue = context.MapMember(member, sourceMemberValue);
         SetDynamically(member, destination, destinationMemberValue);
     }
     return(destination);
 }
Exemplo n.º 10
0
        private static TDestination Map <TDestination>(StringDictionary source, TDestination destination, ResolutionContext context, ProfileMap profileMap)
        {
            var destTypeDetails = profileMap.CreateTypeDetails(typeof(TDestination));

            var memberMatches = from member in destTypeDetails.PublicWriteAccessors
                                join key in source.Keys on member.Name equals key.Trim() into matchingKeys
                                    where matchingKeys.Any()
                                select new { member, sourceNames = matchingKeys };

            object boxedDestination = destination;

            foreach (var match in memberMatches)
            {
                if (match.sourceNames.Count() > 1)
                {
                    throw new AutoMapperMappingException($"Multiple matching keys were found in the source dictionary for destination member {match.member}.", null, new TypePair(typeof(StringDictionary), typeof(TDestination)));
                }

                var value = context.MapMember(match.member, source[match.sourceNames.First()], boxedDestination);
                match.member.SetMemberValue(boxedDestination, value);
            }
            return((TDestination)boxedDestination);
        }
Exemplo n.º 11
0
        private static object MapDynamic(
            StringDictionary source,
            object boxedDestination,
            Type destinationType,
            ResolutionContext context,
            ProfileMap profileMap
            )
        {
            boxedDestination ??= ObjectFactory.CreateInstance(destinationType);
            int matchedCount = 0;

            foreach (var member in profileMap.CreateTypeDetails(destinationType).WriteAccessors)
            {
                var match = MatchSource(member.Name);
                if (match.Count == 0)
                {
                    continue;
                }
                if (match.Count > 1)
                {
                    throw new AutoMapperMappingException(
                              $"Multiple matching keys were found in the source dictionary for destination member {member}.",
                              null,
                              new TypePair(typeof(StringDictionary), destinationType)
                              );
                }
                var value = context.MapMember(member, match.Value, boxedDestination);
                member.SetMemberValue(boxedDestination, value);
                matchedCount++;
            }
            if (matchedCount < source.Count)
            {
                MapInnerProperties();
            }
            return(boxedDestination);

            Match MatchSource(string name)
            {
                if (source.TryGetValue(name, out var value))
                {
                    return(new Match {
                        Value = value, Count = 1
                    });
                }
                var matches = source
                              .Where(s => s.Key.Trim() == name)
                              .Select(s => s.Value)
                              .ToArray();

                if (matches.Length == 1)
                {
                    return(new Match {
                        Value = matches[0], Count = 1
                    });
                }
                return(new Match {
                    Count = matches.Length
                });
            }

            void MapInnerProperties()
            {
                MemberInfo[] innerMembers;
                foreach (var memberPath in source.Keys.Where(k => k.Contains('.')))
                {
                    innerMembers = ReflectionHelper.GetMemberPath(destinationType, memberPath);
                    var innerDestination = GetInnerDestination();
                    if (innerDestination == null)
                    {
                        continue;
                    }
                    var lastMember = innerMembers[innerMembers.Length - 1];
                    var value      = context.MapMember(lastMember, source[memberPath], innerDestination);
                    lastMember.SetMemberValue(innerDestination, value);
                }
                return;

                object GetInnerDestination()
                {
                    var currentDestination = boxedDestination;

                    foreach (var member in innerMembers.Take(innerMembers.Length - 1))
                    {
                        var newDestination = member.GetMemberValue(currentDestination);
                        if (newDestination == null)
                        {
                            if (!member.CanBeSet())
                            {
                                return(null);
                            }
                            newDestination = ObjectFactory.CreateInstance(member.GetMemberType());
                            member.SetMemberValue(currentDestination, newDestination);
                        }
                        currentDestination = newDestination;
                    }
                    return(currentDestination);
                }
            }
        }