Example #1
0
 private static void MapItems(IEnumerable sources, IList results, Type resultType, IObjectMappingResolver customMappings, ObjectMappingConflictResolution conflictResolution)
 {
     if (resultType.IsGenericType)
     {
         Type itemType = resultType.GetGenericArguments().First();
         foreach (object item in sources)
         {
             object result = MapClass(item, item.GetType(), itemType, customMappings, conflictResolution);
             _ = results.Add(result);
         }
     }
     else
     {
         foreach (object item in sources)
         {
             object result = MapClass(item, item.GetType(), item.GetType(), customMappings, conflictResolution);
             _ = results.Add(result);
         }
     }
 }
Example #2
0
        private static void MapProperty(object value, object result, PropertyInfo sourceProperty, PropertyInfo resultProperty, IObjectMappingResolver customMappings, ObjectMappingConflictResolution conflictResolution)
        {
            Type type = resultProperty.PropertyType;

            if (value != null)
            {
                if (type.IsInterface || (type.IsClass && !type.IsAbstract))
                {
                    object convertedVallue = ToClass(value, type, customMappings, conflictResolution);
                    resultProperty.SetValue(result, convertedVallue, null);
                }
                else if (conflictResolution == ObjectMappingConflictResolution.Exception)
                {
                    throw new PropertyMappingException(sourceProperty, resultProperty);
                }
            }
        }
Example #3
0
        private static object MapClass(IEnumerable sources, Type sourceType, Type resultType, IObjectMappingResolver customMappings, ObjectMappingConflictResolution conflictResolution)
        {
            if (!sourceType.IsCollection())
            {
                if (conflictResolution == ObjectMappingConflictResolution.Exception)
                {
                    throw new ClassMappingException(sourceType, resultType);
                }
                return(null);
            }

            int count = sources.Size();

            if (resultType.IsArray)
            {
                Type  itemType = resultType.GetElementType();
                Array results  = Array.CreateInstance(itemType, count);
                MapItems(sources, results, resultType, customMappings, conflictResolution);
                return(results);
            }
            else if (resultType == typeof(IEnumerable) || resultType == typeof(ICollection) || resultType == typeof(IList) ||
                     (resultType.IsGenericType && (resultType.GetGenericTypeDefinition() == typeof(IEnumerable <>) ||
                                                   resultType.GetGenericTypeDefinition() == typeof(ICollection <>) ||
                                                   resultType.GetGenericTypeDefinition() == typeof(IList <>))
                     )
                     )
            {
                if (resultType.IsGenericType)
                {
                    Type itemType = resultType.GetGenericArguments().First();
                    Type listType = typeof(List <>);
                    listType = listType.MakeGenericType(itemType);
                    IList results = Activator.CreateInstance(listType) as IList;
                    MapItems(sources, results, resultType, customMappings, conflictResolution);
                    return(results);
                }
                else
                {
                    Type  listType = typeof(ArrayList);
                    IList results  = Activator.CreateInstance(listType) as IList;
                    MapItems(sources, results, resultType, customMappings, conflictResolution);
                    return(results);
                }
            }
            else if (typeof(IList).IsAssignableFrom(resultType) && resultType.IsClass && !resultType.IsAbstract)
            {
                IList results = Activator.CreateInstance(resultType) as IList;
                MapItems(sources, results, resultType, customMappings, conflictResolution);
                return(results);
            }
            else if (conflictResolution == ObjectMappingConflictResolution.Exception)
            {
                throw new InvalidOperationException();
            }
            return(null);
        }
Example #4
0
        private static void MapItems(IEnumerable sources, Array results, Type resultType, IObjectMappingResolver customMappings, ObjectMappingConflictResolution conflictResolution)
        {
            Type itemType = resultType.GetElementType();
            int  counter  = 0;

            foreach (object item in sources)
            {
                object result = MapClass(item, item.GetType(), itemType, customMappings, conflictResolution);
                results.SetValue(result, counter);
                counter++;
            }
        }
Example #5
0
        private static object ToClass(this object @object, Type resultType, IObjectMappingResolver customMappings, ObjectMappingConflictResolution conflictResolution = ObjectMappingConflictResolution.Exception)
        {
            if (@object == null)
            {
                return(null);
            }
            Type sourceType = @object.GetType();

            if (!resultType.IsInterface && !resultType.IsClass)
            {
                if (conflictResolution == ObjectMappingConflictResolution.Exception)
                {
                    throw new ClassMappingException(sourceType, resultType);
                }
            }

            object result;

#pragma warning disable IDE0045 // Convert to conditional expression
            if (sourceType.IsCollection())
#pragma warning restore IDE0045 // Convert to conditional expression
            {
                result = MapClass((IEnumerable)@object, sourceType, resultType, customMappings, conflictResolution);
            }
            else
            {
                result = MapClass(@object, sourceType, resultType, customMappings, conflictResolution);
            }
            return(result);
        }
Example #6
0
        private static object MapClass(object source, Type sourceType, Type resultType, IObjectMappingResolver customMappings, ObjectMappingConflictResolution conflictResolution)
        {
            if (sourceType.IsCollection())
            {
                if (conflictResolution == ObjectMappingConflictResolution.Exception)
                {
                    throw new ClassMappingException(sourceType, resultType);
                }
                return(null);
            }

            object result = Activator.CreateInstance(resultType);

            PropertyInfo[] sourceProperties = sourceType.GetProperties();
            PropertyInfo[] resultProperties = resultType.GetProperties();

            IClassMappingResolver mappingConfig = customMappings.GetConfiuration(sourceType, resultType);

            foreach (PropertyInfo sourceProperty in sourceProperties)
            {
                string targetName = sourceProperty.Name;
                if (mappingConfig != null)
                {
                    string remappedName = mappingConfig.GetTargetName(sourceProperty.Name);
                    if (remappedName != null)
                    {
                        targetName = remappedName;
                    }
                }
                PropertyInfo resultProperty = resultProperties.FirstOrDefault(x => { return(x.Name == targetName); });
                if (resultProperty == null)
                {
                    continue;
                }
                if (!sourceProperty.CanRead)
                {
                    continue;
                }
                if (!resultProperty.CanWrite)
                {
                    continue;
                }

                object value = sourceProperty.GetValue(source, null);
                if (MappingExpress.CanSetValue(sourceProperty, resultProperty))
                {
                    MappingExpress.SetValue(result, resultProperty, value);
                }
                else
                {
                    Type type = sourceProperty.PropertyType;
                    if (type.IsInterface || type.IsClass)
                    {
                        MapProperty(value, result, sourceProperty, resultProperty, customMappings, conflictResolution);
                    }
                    else if (conflictResolution == ObjectMappingConflictResolution.Exception)
                    {
                        throw new PropertyMappingException(sourceProperty, resultProperty);
                    }
                }
            }
            return(result);
        }
Example #7
0
        /// <summary>
        /// Perform deep copy of current object to desired type. Standard version.
        /// </summary>
        /// <param name="object"></param>
        /// <param name="resultType">Type of the class you want to copy to</param>
        /// <param name="customMappingAction">Callback to create custom mapping for specified type</param>
        /// <param name="conflictResolution">What should be done if a conflict between source type and target type cannot be resolved</param>
        /// <returns></returns>
        public static object ToClass(this object @object, Type resultType, Action <IObjectMappingRegister> customMappingAction = null, ObjectMappingConflictResolution conflictResolution = ObjectMappingConflictResolution.Exception)
        {
            if (@object == null)
            {
                return(null);
            }
            Type sourceType = @object.GetType();

            if (!resultType.IsInterface && !resultType.IsClass)
            {
                if (conflictResolution == ObjectMappingConflictResolution.Exception)
                {
                    throw new ClassMappingException(sourceType, resultType);
                }
            }

            ObjectMappingConfiguration customMappingContainer = new ObjectMappingConfiguration();

            if (customMappingAction != null)
            {
                customMappingAction.Invoke(customMappingContainer);
            }

            object result;

#pragma warning disable IDE0045 // Convert to conditional expression
            if (sourceType.IsCollection())
#pragma warning restore IDE0045 // Convert to conditional expression
            {
                result = MapClass((IEnumerable)@object, sourceType, resultType, customMappingContainer, conflictResolution);
            }
            else
            {
                result = MapClass(@object, sourceType, resultType, customMappingContainer, conflictResolution);
            }
            return(result);
        }
Example #8
0
        /// <summary>
        /// Perform deep copy of current object to desired type. Generic version.
        /// </summary>
        /// <typeparam name="TResult">Type of the class you want to copy to</typeparam>
        /// <param name="object"></param>
        /// <param name="customMappingAction">Callback to create custom mapping for specified type</param>
        /// <param name="conflictResolution">What should be done if a conflict between source type and target type cannot be resolved</param>
        /// <returns></returns>
        public static TResult ToClass <TResult>(this object @object, Action <IObjectMappingRegister> customMappingAction = null, ObjectMappingConflictResolution conflictResolution = ObjectMappingConflictResolution.Exception)
        {
            object result = ToClass(@object, typeof(TResult), customMappingAction, conflictResolution);

            return((TResult)result);
        }
Example #9
0
        public static TResult ToClass <TSource, TResult>(this TSource @object, Action <TSource, TResult> postAction, Action <IObjectMappingRegister> customMappings = null, ObjectMappingConflictResolution conflictResolution = ObjectMappingConflictResolution.Exception)
        {
            TResult result = (TResult)ToClass(@object, typeof(TResult), customMappings, conflictResolution);

            if (@object != null && result != null)
            {
                if (postAction != null)
                {
                    postAction.Invoke(@object, result);
                }
            }
            return(result);
        }
        /// <summary>
        /// Perform deep copy of current object to desired type. Generic version.
        /// </summary>
        /// <typeparam name="TSource">Type of the class you want to copy from</typeparam>
        /// <typeparam name="TResult">Type of the class you want to copy to</typeparam>
        /// <param name="collection"></param>
        /// <param name="postAction"></param>
        /// <param name="customMappingAction">Callback to create custom mapping for specified type</param>
        /// <param name="resolution">What should be done if a conflict between source type and target type cannot be resolved</param>
        /// <returns></returns>
        public static List <TResult> ToList <TSource, TResult>(this IEnumerable <TSource> collection, Action <TSource, TResult> postAction = null, Action <IObjectMappingRegister> customMappingAction = null, ObjectMappingConflictResolution resolution = ObjectMappingConflictResolution.Exception)
        {
            List <TResult> results = new List <TResult>();

            if (collection == null)
            {
                return(results);
            }
            foreach (TSource item in collection)
            {
                TResult result = item.ToClass(postAction, customMappingAction, resolution);
                results.Add(result);
            }
            return(results);
        }
        /// <summary>
        /// Perform deep copy of current object to desired type. Generic version.
        /// </summary>
        /// <typeparam name="TResult">Type of the class you want to copy to</typeparam>
        /// <param name="collection"></param>
        /// <param name="customMappingAction">Callback to create custom mapping for specified type</param>
        /// <param name="resolution">What should be done if a conflict between source type and target type cannot be resolved</param>
        /// <returns></returns>
        public static TResult[] ToArray <TResult>(this IEnumerable collection, Action <IObjectMappingRegister> customMappingAction = null, ObjectMappingConflictResolution resolution = ObjectMappingConflictResolution.Exception)
        {
            ICollection <TResult> results = new List <TResult>();

            if (collection == null)
            {
                return(results.ToArray());
            }
            foreach (object item in collection)
            {
                TResult result = item.ToClass <TResult>(customMappingAction, resolution);
                results.Add(result);
            }
            return(results.ToArray());
        }