/// <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 = context.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 = context.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 (context.Configuration.ReferenceHeaderPrefix != null) { var args = new ReferenceHeaderPrefixArgs { MemberName = referenceMap.Data.Member.Name, MemberType = referenceMap.Data.Member.MemberType(), }; referenceMap.Data.Prefix = context.Configuration.ReferenceHeaderPrefix(args); } } }
/// <summary> /// Auto maps the given map using constructor parameters. /// </summary> /// <param name="map">The map.</param> /// <param name="context">The context.</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, CsvContext context, LinkedList <Type> mapParents, int indexStart = 0) { var type = map.GetGenericType(); var args = new GetConstructorArgs(map.ClassType); var constructor = context.Configuration.GetConstructor(args); var parameters = constructor.GetParameters(); foreach (var parameter in parameters) { var parameterMap = new ParameterMap(parameter); if (parameter.GetCustomAttributes <IgnoreAttribute>(true).Any() || parameter.GetCustomAttributes <ConstantAttribute>(true).Any()) { // If there is an IgnoreAttribute or ConstantAttribute, we still need to add a map because a constructor requires // all parameters to be present. A default value will be used later on. ApplyAttributes(parameterMap); map.ParameterMaps.Add(parameterMap); continue; } var typeConverterType = context.TypeConverterCache.GetConverter(parameter.ParameterType).GetType(); 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 (context.Configuration.IgnoreReferences) { throw new InvalidOperationException($"Configuration '{nameof(CsvConfiguration.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)ObjectResolver.Current.Resolve(refMapType); AutoMapMembers(refMap, context, mapParents, Math.Max(map.GetMaxIndex(isParameter: true) + 1, indexStart)); mapParents.Drop(mapParents.Find(type)); var referenceMap = new ParameterReferenceMap(parameter, refMap); if (context.Configuration.ReferenceHeaderPrefix != null) { var referenceHeaderPrefix = new ReferenceHeaderPrefixArgs(memberTypeInfo.MemberType(), memberTypeInfo.Name); referenceMap.Data.Prefix = context.Configuration.ReferenceHeaderPrefix(referenceHeaderPrefix); } ApplyAttributes(referenceMap); parameterMap.ReferenceMap = referenceMap; } else if (context.Configuration.ShouldUseConstructorParameters(new ShouldUseConstructorParametersArgs(parameter.ParameterType))) { mapParents.AddLast(type); var constructorMapType = typeof(DefaultClassMap <>).MakeGenericType(parameter.ParameterType); var constructorMap = (ClassMap)ObjectResolver.Current.Resolve(constructorMapType); // Need to use Max here for nested types. AutoMapConstructorParameters(constructorMap, context, mapParents, Math.Max(map.GetMaxIndex(isParameter: true) + 1, indexStart)); mapParents.Drop(mapParents.Find(type)); parameterMap.ConstructorTypeMap = constructorMap; } else { parameterMap.Data.TypeConverterOptions = TypeConverterOptions.Merge(new TypeConverterOptions(), context.TypeConverterOptionsCache.GetOptions(parameter.ParameterType), parameterMap.Data.TypeConverterOptions); parameterMap.Data.Index = map.GetMaxIndex(isParameter: true) + 1; ApplyAttributes(parameterMap); } map.ParameterMaps.Add(parameterMap); } map.ReIndex(indexStart); }
/// <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="context">The context.</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, CsvContext context, LinkedList <Type> mapParents, int indexStart = 0) { var type = map.GetGenericType(); var flags = BindingFlags.Instance | BindingFlags.Public; if (context.Configuration.IncludePrivateMembers) { flags = flags | BindingFlags.NonPublic; } var members = new List <MemberInfo>(); if ((context.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 ((context.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) { if (member.GetCustomAttribute <IgnoreAttribute>() != null) { // Ignore this member including its tree if it's a reference. continue; } var typeConverterType = context.TypeConverterCache.GetConverter(member).GetType(); if (context.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 (context.Configuration.IgnoreReferences) { continue; } if (CheckForCircularReference(member.MemberType(), mapParents)) { continue; } mapParents.AddLast(type); var refMapType = typeof(DefaultClassMap <>).MakeGenericType(member.MemberType()); var refMap = (ClassMap)ObjectResolver.Current.Resolve(refMapType); if (memberTypeInfo.HasConstructor() && !memberTypeInfo.HasParameterlessConstructor() && !memberTypeInfo.IsUserDefinedStruct()) { AutoMapConstructorParameters(refMap, context, mapParents, Math.Max(map.GetMaxIndex() + 1, indexStart)); } // Need to use Max here for nested types. AutoMapMembers(refMap, context, 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 (context.Configuration.ReferenceHeaderPrefix != null) { var args = new ReferenceHeaderPrefixArgs(member.MemberType(), member.Name); referenceMap.Data.Prefix = context.Configuration.ReferenceHeaderPrefix(args); } 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(), context.TypeConverterOptionsCache.GetOptions(member.MemberType()), memberMap.Data.TypeConverterOptions); memberMap.Data.Index = map.GetMaxIndex() + 1; ApplyAttributes(memberMap); map.MemberMaps.Add(memberMap); } } map.ReIndex(indexStart); }