Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 14 additions & 11 deletions LINQtoCSV/FieldMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public override string ToString()

// IndexToInfo is used to quickly translate the index of a field
// to its TypeFieldInfo.
protected TypeFieldInfo[] m_IndexToInfo = null;
protected List<TypeFieldInfo> m_IndexToInfo = null;

/// <summary>
/// Contains a mapping between the CSV column indexes that will read and the property indexes in the business object.
Expand Down Expand Up @@ -200,14 +200,14 @@ protected void AnalyzeType(
// Initialize IndexToInfo

int nbrTypeFields = m_NameToInfo.Keys.Count;
m_IndexToInfo = new TypeFieldInfo[nbrTypeFields];
m_IndexToInfo = new List<TypeFieldInfo>(nbrTypeFields);

_mappingIndexes = new Dictionary<int, int>();

int i=0;
foreach (KeyValuePair<string, TypeFieldInfo> kvp in m_NameToInfo)
{
m_IndexToInfo[i++] = kvp.Value;
m_IndexToInfo.Insert(i++,kvp.Value);
}

// Sort by FieldIndex. Fields without FieldIndex will
Expand All @@ -221,7 +221,7 @@ protected void AnalyzeType(
// Note that for reading from a file with field names in the
// first line, method ReadNames reworks IndexToInfo.

Array.Sort(m_IndexToInfo);
m_IndexToInfo.Sort();

// ----------
// Make sure there are no duplicate FieldIndices.
Expand Down Expand Up @@ -286,7 +286,7 @@ public void WriteNames(List<string> row)
{
row.Clear();

for (int i = 0; i < m_IndexToInfo.Length; i++)
for (int i = 0; i < m_IndexToInfo.Count; i++)
{
TypeFieldInfo tfi = m_IndexToInfo[i];

Expand All @@ -310,7 +310,7 @@ public void WriteObject(T obj, List<string> row)
{
row.Clear();

for (int i = 0; i < m_IndexToInfo.Length; i++)
for (int i = 0; i < m_IndexToInfo.Count; i++)
{
TypeFieldInfo tfi = m_IndexToInfo[i];

Expand Down Expand Up @@ -433,9 +433,12 @@ public void ReadNames(IDataRow row)
continue;
}

m_IndexToInfo[_mappingIndexes[i]] = m_NameToInfo[row[i].Value];
TypeFieldInfo tfi = m_NameToInfo[row[i].Value];
m_IndexToInfo.Remove(tfi);
m_IndexToInfo.Insert(_mappingIndexes[i], tfi);

if (m_fileDescription.EnforceCsvColumnAttribute && (!m_IndexToInfo[i].hasColumnAttribute)) {
if (m_fileDescription.EnforceCsvColumnAttribute && !tfi.hasColumnAttribute)
{
// enforcing column attr, but this field/prop has no column attr.
throw new MissingCsvColumnAttributeException(typeof (T).ToString(), row[i].Value, m_fileName);
}
Expand All @@ -462,7 +465,7 @@ public List<int> GetCharLengths()
/// <returns></returns>
public T ReadObject(IDataRow row, AggregatedException ae) {
//If there are more columns than the required
if (row.Count > m_IndexToInfo.Length)
if (row.Count > m_IndexToInfo.Count)
{
//Are we ignoring unknown columns?
if (!m_fileDescription.IgnoreUnknownColumns) {
Expand All @@ -476,7 +479,7 @@ public T ReadObject(IDataRow row, AggregatedException ae) {
T obj = new T();

//If we will be using the mappings, we just iterate through all the cells in this row
int maxRowCount = _mappingIndexes.Count > 0 ? row.Count : Math.Min(row.Count, m_IndexToInfo.Length);
int maxRowCount = _mappingIndexes.Count > 0 ? row.Count : Math.Min(row.Count, m_IndexToInfo.Count);

for (int i = 0; i < maxRowCount; i++) {
TypeFieldInfo tfi;
Expand Down Expand Up @@ -621,7 +624,7 @@ public T ReadObject(IDataRow row, AggregatedException ae) {
// If only looking at fields with CsvColumn attribute, do ignore
// fields that don't have that attribute.

for (int i = row.Count; i < m_IndexToInfo.Length; i++)
for (int i = row.Count; i < m_IndexToInfo.Count; i++)
{
TypeFieldInfo tfi = m_IndexToInfo[i];

Expand Down