blob: f66a2846379e1c5e674c3d05ad90a0449eaaa524 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
|
// 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
{
/// <summary>
/// Collection that holds CsvClassMaps for record types.
/// </summary>
public class ClassMapCollection
{
private readonly Dictionary<Type, ClassMap> data = new Dictionary<Type, ClassMap>();
private readonly CsvContext context;
/// <summary>
/// Gets the <see cref="ClassMap"/> for the specified record type.
/// </summary>
/// <value>
/// The <see cref="ClassMap"/>.
/// </value>
/// <param name="type">The record type.</param>
/// <returns>The <see cref="ClassMap"/> for the specified record type.</returns>
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;
}
}
}
}
/// <summary>
/// Creates a new instance using the given configuration.
/// </summary>
/// <param name="context">The context.</param>
public ClassMapCollection(CsvContext context)
{
this.context = context;
}
/// <summary>
/// Finds the <see cref="ClassMap"/> for the specified record type.
/// </summary>
/// <typeparam name="T">The record type.</typeparam>
/// <returns>The <see cref="ClassMap"/> for the specified record type.</returns>
public virtual ClassMap<T>? Find<T>()
{
return (ClassMap<T>?)this[typeof(T)];
}
/// <summary>
/// Adds the specified map for it's record type. If a map
/// already exists for the record type, the specified
/// map will replace it.
/// </summary>
/// <param name="map">The map.</param>
internal virtual void Add(ClassMap map)
{
SetMapDefaults(map);
var type = GetGenericCsvClassMapType(map.GetType()).GetGenericArguments().First();
data[type] = map;
}
/// <summary>
/// Removes the class map.
/// </summary>
/// <param name="classMapType">The class map type.</param>
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);
}
/// <summary>
/// Removes all maps.
/// </summary>
internal virtual void Clear()
{
data.Clear();
}
/// <summary>
/// Goes up the inheritance tree to find the type instance of CsvClassMap{}.
/// </summary>
/// <param name="type">The type to traverse.</param>
/// <returns>The type that is CsvClassMap{}.</returns>
private Type GetGenericCsvClassMapType(Type type)
{
if (type.GetTypeInfo().IsGenericType && type.GetGenericTypeDefinition() == typeof(ClassMap<>))
{
return type;
}
return GetGenericCsvClassMapType(type.GetTypeInfo().BaseType);
}
/// <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.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);
}
}
}
}
}
|