Beispiel #1
0
        /// <summary>
        /// Sets defaults for the mapping tree. The defaults used
        /// to be set inside the classes, but this didn't allow for
        /// the TypeConverter to be created from the Configuration's
        /// TypeConverterFactory.
        /// </summary>
        /// <param name="map">The map to set defaults on.</param>
        private void SetMapDefaults(ClassMap map)
        {
            foreach (var parameterMap in map.ParameterMaps)
            {
                if (parameterMap.ConstructorTypeMap != null)
                {
                    SetMapDefaults(parameterMap.ConstructorTypeMap);
                }
                else if (parameterMap.ReferenceMap != null)
                {
                    SetMapDefaults(parameterMap.ReferenceMap.Data.Mapping);
                }
                else
                {
                    if (parameterMap.Data.TypeConverter == null)
                    {
                        parameterMap.Data.TypeConverter = configuration.TypeConverterCache.GetConverter(parameterMap.Data.Parameter.ParameterType);
                    }

                    if (parameterMap.Data.Names.Count == 0)
                    {
                        parameterMap.Data.Names.Add(parameterMap.Data.Parameter.Name);
                    }
                }
            }

            foreach (var memberMap in map.MemberMaps)
            {
                if (memberMap.Data.Member == null)
                {
                    continue;
                }

                if (memberMap.Data.TypeConverter == null)
                {
                    memberMap.Data.TypeConverter = configuration.TypeConverterCache.GetConverter(memberMap.Data.Member.MemberType());
                }

                if (memberMap.Data.Names.Count == 0)
                {
                    memberMap.Data.Names.Add(memberMap.Data.Member.Name);
                }
            }

            foreach (var referenceMap in map.ReferenceMaps)
            {
                SetMapDefaults(referenceMap.Data.Mapping);

                if (configuration.ReferenceHeaderPrefix != null)
                {
                    referenceMap.Data.Prefix = configuration.ReferenceHeaderPrefix(referenceMap.Data.Member.MemberType(), referenceMap.Data.Member.Name);
                }
            }
        }
Beispiel #2
0
        /// <summary>
        /// Auto maps the given map using constructor parameters.
        /// </summary>
        /// <param name="map">The map.</param>
        /// <param name="configuration">The configuration.</param>
        /// <param name="mapParents">The list of parents for the map.</param>
        /// <param name="indexStart">The index starting point.</param>
        protected virtual void AutoMapConstructorParameters(ClassMap map, CsvConfiguration configuration, LinkedList <Type> mapParents, int indexStart = 0)
        {
            var type        = map.GetGenericType();
            var constructor = configuration.GetConstructor(map.ClassType);
            var parameters  = constructor.GetParameters();

            foreach (var parameter in parameters)
            {
                var typeConverterType = configuration.TypeConverterCache.GetConverter(parameter.ParameterType).GetType();

                var parameterMap = new ParameterMap(parameter);

                var memberTypeInfo     = parameter.ParameterType.GetTypeInfo();
                var isDefaultConverter = typeConverterType == typeof(DefaultTypeConverter);
                if (isDefaultConverter && (memberTypeInfo.HasParameterlessConstructor() || memberTypeInfo.IsUserDefinedStruct()))
                {
                    // If the type is not one covered by our type converters
                    // and it has a parameterless constructor, create a
                    // reference map for it.

                    if (configuration.IgnoreReferences)
                    {
                        throw new InvalidOperationException($"Configuration '{nameof(configuration.IgnoreReferences)}' can't be true " +
                                                            "when using types without a default constructor. Constructor parameters " +
                                                            "are used and all members including references must be used.");
                    }

                    if (CheckForCircularReference(parameter.ParameterType, mapParents))
                    {
                        throw new InvalidOperationException($"A circular reference was detected in constructor paramter '{parameter.Name}'." +
                                                            "Since all parameters must be supplied for a constructor, this parameter can't be skipped.");
                    }

                    mapParents.AddLast(type);
                    var refMapType = typeof(DefaultClassMap <>).MakeGenericType(parameter.ParameterType);
                    var refMap     = (ClassMap)ReflectionHelper.CreateInstance(refMapType);
                    AutoMapMembers(refMap, configuration, mapParents, Math.Max(map.GetMaxIndex() + 1, indexStart));
                    mapParents.Drop(mapParents.Find(type));

                    var referenceMap = new ParameterReferenceMap(parameter, refMap);
                    if (configuration.ReferenceHeaderPrefix != null)
                    {
                        referenceMap.Data.Prefix = configuration.ReferenceHeaderPrefix(memberTypeInfo.MemberType(), memberTypeInfo.Name);
                    }

                    parameterMap.ReferenceMap = referenceMap;
                }
                else if (configuration.ShouldUseConstructorParameters(parameter.ParameterType))
                {
                    mapParents.AddLast(type);
                    var constructorMapType = typeof(DefaultClassMap <>).MakeGenericType(parameter.ParameterType);
                    var constructorMap     = (ClassMap)ReflectionHelper.CreateInstance(constructorMapType);
                    // Need to use Max here for nested types.
                    AutoMapConstructorParameters(constructorMap, configuration, mapParents, Math.Max(map.GetMaxIndex() + 1, indexStart));
                    mapParents.Drop(mapParents.Find(type));

                    parameterMap.ConstructorTypeMap = constructorMap;
                }
                else
                {
                    parameterMap.Data.TypeConverterOptions = TypeConverterOptions.Merge(new TypeConverterOptions(), configuration.TypeConverterOptionsCache.GetOptions(parameter.ParameterType), parameterMap.Data.TypeConverterOptions);
                    parameterMap.Data.Index = map.GetMaxIndex() + 1;
                }

                map.ParameterMaps.Add(parameterMap);
            }

            map.ReIndex(indexStart);
        }
Beispiel #3
0
        /// <summary>
        /// Auto maps the given map and checks for circular references as it goes.
        /// </summary>
        /// <param name="map">The map to auto map.</param>
        /// <param name="configuration">The configuration.</param>
        /// <param name="mapParents">The list of parents for the map.</param>
        /// <param name="indexStart">The index starting point.</param>
        protected virtual void AutoMapMembers(ClassMap map, CsvConfiguration configuration, LinkedList <Type> mapParents, int indexStart = 0)
        {
            var type = map.GetGenericType();

            var flags = BindingFlags.Instance | BindingFlags.Public;

            if (configuration.IncludePrivateMembers)
            {
                flags = flags | BindingFlags.NonPublic;
            }

            var members = new List <MemberInfo>();

            if ((configuration.MemberTypes & MemberTypes.Properties) == MemberTypes.Properties)
            {
                // We need to go up the declaration tree and find the actual type the property
                // exists on and use that PropertyInfo instead. This is so we can get the private
                // set method for the property.
                var properties = new List <PropertyInfo>();
                foreach (var property in ReflectionHelper.GetUniqueProperties(type, flags))
                {
                    if (properties.Any(p => p.Name == property.Name))
                    {
                        // Multiple properties could have the same name if a child class property
                        // is hiding a parent class property by using `new`. It's possible that
                        // the order of the properties returned
                        continue;
                    }

                    properties.Add(ReflectionHelper.GetDeclaringProperty(type, property, flags));
                }

                members.AddRange(properties);
            }

            if ((configuration.MemberTypes & MemberTypes.Fields) == MemberTypes.Fields)
            {
                var fields = new List <MemberInfo>();
                foreach (var field in type.GetFields(flags))
                {
                    if (!field.GetCustomAttributes(typeof(CompilerGeneratedAttribute), false).Any())
                    {
                        fields.Add(field);
                    }
                }

                members.AddRange(fields);
            }

            foreach (var member in members)
            {
                var typeConverterType = configuration.TypeConverterCache.GetConverter(member).GetType();

                if (configuration.HasHeaderRecord && enumerableConverters.Contains(typeConverterType))
                {
                    // Enumerable converters can't write the header properly, so skip it.
                    continue;
                }

                var memberTypeInfo     = member.MemberType().GetTypeInfo();
                var isDefaultConverter = typeConverterType == typeof(DefaultTypeConverter);
                if (isDefaultConverter)
                {
                    // If the type is not one covered by our type converters
                    // and it has a parameterless constructor, create a
                    // reference map for it.

                    if (configuration.IgnoreReferences)
                    {
                        continue;
                    }

                    if (CheckForCircularReference(member.MemberType(), mapParents))
                    {
                        continue;
                    }

                    mapParents.AddLast(type);
                    var refMapType = typeof(DefaultClassMap <>).MakeGenericType(member.MemberType());
                    var refMap     = (ClassMap)ReflectionHelper.CreateInstance(refMapType);

                    if (memberTypeInfo.HasConstructor() && !memberTypeInfo.HasParameterlessConstructor() && !memberTypeInfo.IsUserDefinedStruct())
                    {
                        AutoMapConstructorParameters(refMap, configuration, mapParents, Math.Max(map.GetMaxIndex() + 1, indexStart));
                    }

                    // Need to use Max here for nested types.
                    AutoMapMembers(refMap, configuration, mapParents, Math.Max(map.GetMaxIndex() + 1, indexStart));
                    mapParents.Drop(mapParents.Find(type));

                    if (refMap.MemberMaps.Count > 0 || refMap.ReferenceMaps.Count > 0)
                    {
                        var referenceMap = new MemberReferenceMap(member, refMap);
                        if (configuration.ReferenceHeaderPrefix != null)
                        {
                            referenceMap.Data.Prefix = configuration.ReferenceHeaderPrefix(member.MemberType(), member.Name);
                        }

                        ApplyAttributes(referenceMap);

                        map.ReferenceMaps.Add(referenceMap);
                    }
                }
                else
                {
                    // Only add the member map if it can be converted later on.
                    // If the member will use the default converter, don't add it because
                    // we don't want the .ToString() value to be used when auto mapping.

                    // Use the top of the map tree. This will maps that have been auto mapped
                    // to later on get a reference to a map by doing map.Map( m => m.A.B.C.Id )
                    // and it will return the correct parent map type of A instead of C.
                    var classType = mapParents.First?.Value ?? map.ClassType;
                    var memberMap = MemberMap.CreateGeneric(classType, member);

                    // Use global values as the starting point.
                    memberMap.Data.TypeConverterOptions = TypeConverterOptions.Merge(new TypeConverterOptions(), configuration.TypeConverterOptionsCache.GetOptions(member.MemberType()), memberMap.Data.TypeConverterOptions);
                    memberMap.Data.Index = map.GetMaxIndex() + 1;

                    ApplyAttributes(memberMap);

                    map.MemberMaps.Add(memberMap);
                }
            }

            map.ReIndex(indexStart);
        }