Пример #1
0
        /// <summary>
        /// Add simple or complex item that maps source member to destination member.
        /// </summary>
        /// <typeparam name="TSourceMember"></typeparam>
        /// <typeparam name="TDestinationMember"></typeparam>
        /// <param name="source">The source<see cref="TSourceMember"/></param>
        /// <param name="destination">The destination<see cref="TDestinationMember"/></param>
        /// <param name="complex">The complex<see cref="bool"/></param>
        /// <returns>The <see cref="SimpleMapper"/></returns>
        private SimpleMapper AddItem <TSourceMember, TDestinationMember>(TSourceMember source, TDestinationMember destination, bool complex)
            where TSourceMember : MemberInfo
            where TDestinationMember : MemberInfo
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            if (destination == null)
            {
                throw new ArgumentNullException(nameof(destination));
            }

            var sourceItem      = new MappedItem.Item(source, true);
            var destinationItem = new MappedItem.Item(destination, false);

            MappedItem mapping = new MappedItem(sourceItem, destinationItem, this);

            this.items.Add(mapping);

            if (complex)
            {
                SimpleMapper subMap = MapResolver.Resolve(sourceItem.Type, destinationItem.Type);

                mapping.IsComplex = true;

                subMaps.Add(mapping.Key, subMap);
            }

            return(this);
        }
        /// <summary>
        /// Maps source object to destination object.
        /// </summary>
        /// <param name="source">The source<see cref="object"/></param>
        /// <param name="destination">The destination<see cref="object"/></param>
        /// <param name="sourceType">The sourceType<see cref="Type"/></param>
        /// <param name="destinationType">The destinationType<see cref="Type"/></param>
        private static void Map(object source, object destination, Type sourceType, Type destinationType)
        {
            //// get new or previously resolved map
            SimpleMapper map = MapResolver.Resolve(sourceType, destinationType);

            //// map source to destination
            Map(map, source, destination, sourceType, destinationType);
        }
Пример #3
0
        /// <summary>
        /// Add simple or complex item that maps source member to destination member.
        /// </summary>
        /// <param name="sourceMemberName">The sourceMemberName<see cref="string"/></param>
        /// <param name="sourceType">The sourceType<see cref="Type"/></param>
        /// <param name="destinationMemberName">The destinationMemberName<see cref="string"/></param>
        /// <param name="destinationType">The destinationType<see cref="Type"/></param>
        /// <param name="complex">The complex<see cref="bool"/></param>
        /// <returns>The <see cref="SimpleMapper"/></returns>
        private SimpleMapper AddItem(string sourceMemberName, Type sourceType, string destinationMemberName, Type destinationType, bool complex)
        {
            if (string.IsNullOrWhiteSpace(sourceMemberName))
            {
                throw new ArgumentNullException(nameof(sourceMemberName));
            }

            if (string.IsNullOrWhiteSpace(destinationMemberName))
            {
                throw new ArgumentNullException(nameof(destinationMemberName));
            }

            if (sourceType == null)
            {
                throw new ArgumentNullException(nameof(sourceType));
            }

            if (destinationType == null)
            {
                throw new ArgumentNullException(nameof(destinationType));
            }

            MemberInfo sm = sourceType.GetMember(sourceMemberName).FirstOrDefault();
            MemberInfo dm = destinationType.GetMember(destinationMemberName).FirstOrDefault();

            MappedItem.Item sourceItem      = null;
            MappedItem.Item destinationItem = null;
            MappedItem      mapping         = null;

            if (sm != null && dm != null)
            {
                sourceItem      = new MappedItem.Item(sm, true);
                destinationItem = new MappedItem.Item(dm, false);
                mapping         = new MappedItem(sourceItem, destinationItem, this);

                this.items.Add(mapping);
            }

            if (mapping != null)
            {
                if (complex)
                {
                    SimpleMapper subMap = MapResolver.Resolve(sourceItem.Type, destinationItem.Type);

                    mapping.IsComplex = true;

                    subMaps.Add(mapping.Key, subMap);
                }
            }

            return(this);
        }
 /// <summary>
 /// Resolves <see cref="ISimpleMapper"/> between source and destination type.
 /// </summary>
 /// <param name="sourceType">The sourceType<see cref="Type"/></param>
 /// <param name="destinationType">The destinationType<see cref="Type"/></param>
 /// <param name="options">The options<see cref="MapResolveOptions"/></param>
 /// <returns>A resolved map.</returns>
 public static ISimpleMapper Resolve(Type sourceType, Type destinationType, MapResolveOptions options = MapResolveOptions.None)
 {
     return(MapResolver.Resolve(sourceType, destinationType, options));
 }
 /// <summary>
 /// Resolves <see cref="ISimpleMapper"/> between source and destination type.
 /// </summary>
 /// <typeparam name="TSource"></typeparam>
 /// <typeparam name="TDestination"></typeparam>
 /// <param name="options">The options<see cref="MapResolveOptions"/></param>
 /// <returns>A resolved map.</returns>
 public static ISimpleMapper Resolve <TSource, TDestination>(MapResolveOptions options = MapResolveOptions.None) where TDestination : new()
 {
     return(MapResolver.Resolve <TSource, TDestination>(options));
 }