// Copyright 2009-2022 Josh Close // This file is a part of CsvHelper and is dual licensed under MS-PL and Apache 2.0. // See LICENSE.txt for details or visit http://www.opensource.org/licenses/ms-pl.html for MS-PL and http://opensource.org/licenses/Apache-2.0 for Apache 2.0. // https://github.com/JoshClose/CsvHelper using System; using System.Collections.Generic; using System.Linq; using System.Reflection; namespace CsvHelper.Configuration { /// /// Collection that holds CsvClassMaps for record types. /// public class ClassMapCollection { private readonly Dictionary data = new Dictionary(); private readonly CsvContext context; /// /// Gets the for the specified record type. /// /// /// The . /// /// The record type. /// The for the specified record type. public virtual ClassMap? this[Type type] { get { // Go up the inheritance tree to find the matching type. // We can't use IsAssignableFrom because both a child // and it's parent/grandparent/etc could be mapped. var currentType = type; while (true) { if (data.TryGetValue(currentType, out var map)) { return map; } currentType = currentType.GetTypeInfo().BaseType; if (currentType == null) { return null; } } } } /// /// Creates a new instance using the given configuration. /// /// The context. public ClassMapCollection(CsvContext context) { this.context = context; } /// /// Finds the for the specified record type. /// /// The record type. /// The for the specified record type. public virtual ClassMap? Find() { return (ClassMap?)this[typeof(T)]; } /// /// Adds the specified map for it's record type. If a map /// already exists for the record type, the specified /// map will replace it. /// /// The map. internal virtual void Add(ClassMap map) { SetMapDefaults(map); var type = GetGenericCsvClassMapType(map.GetType()).GetGenericArguments().First(); data[type] = map; } /// /// Removes the class map. /// /// The class map type. internal virtual void Remove(Type classMapType) { if (!typeof(ClassMap).IsAssignableFrom(classMapType)) { throw new ArgumentException("The class map type must inherit from CsvClassMap."); } var type = GetGenericCsvClassMapType(classMapType).GetGenericArguments().First(); data.Remove(type); } /// /// Removes all maps. /// internal virtual void Clear() { data.Clear(); } /// /// Goes up the inheritance tree to find the type instance of CsvClassMap{}. /// /// The type to traverse. /// The type that is CsvClassMap{}. private Type GetGenericCsvClassMapType(Type type) { if (type.GetTypeInfo().IsGenericType && type.GetGenericTypeDefinition() == typeof(ClassMap<>)) { return type; } return GetGenericCsvClassMapType(type.GetTypeInfo().BaseType); } /// /// 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. /// /// The map to set defaults on. 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.ReadingConvertExpression == null && memberMap.Data.WritingConvertExpression == 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(referenceMap.Data.Member.MemberType(), referenceMap.Data.Member.Name); referenceMap.Data.Prefix = context.Configuration.ReferenceHeaderPrefix(args); } } } } }