private IEnumerable <T> ReadData <T>( string fileName, StreamReader stream, CsvFileDescription fileDescription) where T : class, ICsvRow, new() { bool readingRawDataRows = typeof(IDataRow).IsAssignableFrom(typeof(T)); FieldMapper_Reading <T> fm = null; if (!readingRawDataRows) { fm = new FieldMapper_Reading <T>(fileDescription, fileName, false); } bool readingFile = !string.IsNullOrEmpty(fileName); if (readingFile) { stream = new StreamReader(new FileStream(fileName, FileMode.Open, FileAccess.Read)); } else { if ((stream == null) || (!stream.BaseStream.CanSeek)) { throw new BadStreamException(); } stream.BaseStream.Seek(0, SeekOrigin.Begin); } CsvStream cs = new CsvStream(stream, null, fileDescription.SeparatorChar); IDataRow row = null; if (readingRawDataRows) { row = new T() as IDataRow; } else { row = new DataRow(); } AggregatedException ae = new AggregatedException(typeof(T).ToString(), fileName, fileDescription.MaximumNbrExceptions); try { bool firstRow = true; while (cs.ReadRow(ref row)) { if ((row.Count == 1) && ((row[0].Value == null) || (string.IsNullOrEmpty(row[0].Value.Trim())))) { continue; } if (firstRow && fileDescription.FirstLineHasColumnNames) { if (!readingRawDataRows) { fm.ReadNames(row); } } else { T obj = default(T); try { if (readingRawDataRows) { obj = row as T; } else { obj = fm.ReadObject(row, ae); } } catch (AggregatedException ae2) { throw ae2; } catch (Exception e) { ae.AddException(e); } yield return(obj); } firstRow = false; } } finally { if (readingFile) { stream.Dispose(); } ae.ThrowIfExceptionsStored(); } }
public T ReadObject(IDataRow row, AggregatedException ae) { T obj = new T(); obj.DynamicResourceList = new List <DynamicResource>(); List <TypeFieldInfo> list = m_IndexToInfo.ToList(); for (int i = 0; i < row.Count; i++) { TypeFieldInfo tfi = list.Where(tf => tf.name == rowName[i]).SingleOrDefault(); if (tfi != null) { if (m_fileDescription.EnforceCsvColumnAttribute && (!tfi.hasColumnAttribute)) { throw new TooManyNonCsvColumnDataFieldsException(typeof(T).ToString(), row[i].LineNbr, m_fileName); } if ((!m_fileDescription.FirstLineHasColumnNames) && (tfi.index == CsvColumnAttribute.mc_DefaultFieldIndex)) { throw new MissingFieldIndexException(typeof(T).ToString(), row[i].LineNbr, m_fileName); } string value = row[i].Value; if (value == null) { if (!tfi.canBeNull) { ae.AddException( new MissingRequiredFieldException( typeof(T).ToString(), tfi.name, row[i].LineNbr, m_fileName)); } } else { try { Object objValue = null; if (tfi.typeConverter != null) { objValue = tfi.typeConverter.ConvertFromString( null, m_fileDescription.FileCultureInfo, value); } else if (tfi.parseNumberMethod != null) { if (!string.IsNullOrWhiteSpace(value)) { value = value.Replace('.', ','); value = Math.Round(decimal.Parse(value.ToString()), 3).ToString(); } if (string.IsNullOrWhiteSpace(value)) { value = "0"; } if (value.Contains("E") || value.Contains("e")) { value = value.Replace('.', ','); value = Convert.ToDouble(value).ToString(); } objValue = tfi.parseNumberMethod.Invoke( tfi.fieldType, new Object[] { value, tfi.inputNumberStyle, m_fileDescription.FileCultureInfo }); } else { objValue = value; } if (tfi.memberInfo is PropertyInfo) { ((PropertyInfo)tfi.memberInfo).SetValue(obj, objValue, null); } else { ((FieldInfo)tfi.memberInfo).SetValue(obj, objValue); } } catch (Exception e) { if (e is TargetInvocationException) { e = e.InnerException; } if (e is FormatException) { e = new WrongDataFormatException( typeof(T).ToString(), tfi.name, value, row[i].LineNbr, m_fileName, e); } ae.AddException(e); } } } else { if (!string.IsNullOrWhiteSpace(row[i].Value)) { obj.DynamicResourceList.Add(new DynamicResource() { Key = rowName[i], Value = row[i].Value }); } } } for (int i = row.Count; i < m_IndexToInfo.Length; i++) { TypeFieldInfo tfi = m_IndexToInfo[i]; if (((!m_fileDescription.EnforceCsvColumnAttribute) || tfi.hasColumnAttribute) && (!tfi.canBeNull)) { ae.AddException( new MissingRequiredFieldException( typeof(T).ToString(), tfi.name, row[row.Count - 1].LineNbr, m_fileName)); } } return(obj); }