public void TestInitialize() { _description = new CsvFileDescription { SeparatorChar = ',', FirstLineHasColumnNames = true, IgnoreUnknownColumns = true, }; _input = @"Id,Name,Last Name,Age,City 1,John,Doe,15,Washington"; byte[] stringAsByteArray = Encoding.UTF8.GetBytes(_input); Stream stream = new MemoryStream(stringAsByteArray); _sr = new StreamReader(stream, Encoding.UTF8); _dataAccess = new FileDataAccess(_sr, _description); _cs = new CsvStream(_sr, null, _description.SeparatorChar, _description.IgnoreTrailingSeparatorChar); _row = new DataRow(); _fm = new FieldMapperReading <Person>(_description, null, false); _ae = new AggregatedException(typeof(Person).ToString(), null, _description.MaximumNbrExceptions); _reader = new RowReader <Person>(_description, _ae); }
/// <summary> /// Raise event for DaySchedule change event /// </summary> /// <param name="c"></param> protected void RaiseDayScheduleChangeEvent(object c) { DayScheduleChangeEventHandle t = dayScheduleChangeEventHandle; if (t == null) { return; } var exlist = new AggregatedException(); foreach (DayScheduleChangeEventHandle ev in t.GetInvocationList()) { try { ev(this, new DayScheduleChangedEventArgs(c)); } catch (Exception ex) { exlist.Add(ex); } } if (exlist.Count > 0) { throw exlist; } }
/// <summary> /// Invoke the event handler /// </summary> /// <param name="sender"></param> /// <param name="e"></param> public void Invoke(object sender, EventArgs e) { //if (this._eventHandle == default(T)) // throw new ArgumentException("EventHandler is unassigned"); //if (this._eventArgs == default(U)) // throw new ArgumentException("EventArgs is unassigned"); var handlers = _eventHandle as EventHandler; if (handlers == null) { return; } var exlist = new AggregatedException(); foreach (EventHandler ev in handlers.GetInvocationList()) { try { ev(sender, e); } catch (Exception ex) { exlist.Add(ex); } } if (exlist.Count > 0) { throw exlist; } }
/// <summary> /// Raise a workstatus event /// </summary> /// <param name="message"></param> /// <param name="index"></param> /// <param name="percentage"></param> protected void RaiseWorkStatus(string message, int index, decimal percentage) { // make a local copy for thread-safe WorkStatusEventHandler t = workStatusEvent; if (t == null) { return; } var exlist = new AggregatedException(); foreach (WorkStatusEventHandler ev in t.GetInvocationList()) { try { ev(this, new WorkStatusEventArgs(message, ThreadName, AbsoluteIndex(index), percentage, WorkerThreadType.Slave)); } catch (Exception ex) { exlist.Add(ex); } } if (exlist.Count > 0) { throw exlist; } }
/// <summary> /// Raise the Stop event /// </summary> protected void RaiseStopEvent() { EventHandler t = stopEvent; if (t == null) { return; } var exlist = new AggregatedException(); foreach (EventHandler ev in t.GetInvocationList()) { try { lock (lockObjStop) { ev(this, EventArgs.Empty); } } catch (Exception ex) { exlist.Add(ex); } } if (exlist.Count > 0) { throw exlist; } }
public RowReader <T> ReadDataPreparation <T>(string fileName) where T : class, new() { RewindStream(fileName); Ae = new AggregatedException(typeof(T).ToString(), fileName, FileDescription.MaximumNbrExceptions); return(new RowReader <T>(FileDescription, Ae)); }
/// <summary> /// Publish or raise the event. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void RaiseChangeEvent(object sender, ChangeEventArgs e) { // make a local copy for thread-safe. var t = OnChanged; var exlist = new AggregatedException(); if (t == null) { return; } foreach (ChangeEventHandler ev in t.GetInvocationList()) { try { ev(sender, e); } catch (Exception ex) { exlist.Add(ex); } } if (exlist.Count > 0) { throw exlist; } }
public void TestCreateWithNoMessageSingleException() { var ex = new AggregatedException(BA(GetException("one"))); Assert.That(ex.Message, Is .StringStarting("-------------------------------------" + Environment.NewLine + Environment.NewLine + Environment.NewLine) .And .StringContaining("one") .And .StringEnding("-------------------------------------" + Environment.NewLine)); }
private void btnHelp_Click(object sender, EventArgs e) { try { var tmp = OnHelpClick; if (tmp == null) { return; } FormUtil.Busy(this, true); var exlist = new AggregatedException(); foreach (WizardEventHandler eh in tmp.GetInvocationList()) { try { eh(sender, new WizardEventArgs(currentStep)); } catch (Exception ex) { exlist.Add(ex); } } if (exlist.Count > 0) { throw exlist; } } catch (Exception ex) { FormUtil.WinException(ex); } finally { FormUtil.Busy(this, false); } }
private void RaiseExecuteEventHandlers() { try { var tmp = OnExecute; if (tmp == null) { return; } FormUtil.Busy(this, true); var exlist = new AggregatedException(); foreach (WizardEventHandler eh in tmp.GetInvocationList()) { try { eh(this, new WizardEventArgs(currentStep)); } catch (Exception ex) { exlist.Add(ex); } } if (exlist.Count > 0) { throw exlist; } } catch (Exception ex) { FormUtil.WinException(ex); } finally { FormUtil.Busy(this, false); } }
public T ReadObject(IDataRow row, AggregatedException ae) { if (row.Count > fieldIndexInfo.IndexToInfo.Length) { //Are we ignoring unknown columns? if (!FileDescription.IgnoreUnknownColumns) { // Too many fields throw new TooManyDataFieldsException(typeof(T).ToString(), row[0].LineNbr, FileName); } } var obj = new T(); //If we will be using the mappings, we just iterate through all the cells in this row var maxRowCount = fieldIndexInfo.GetMaxRowCount(row.Count); for (var i = 0; i < maxRowCount; i++) { var tfi = fieldIndexInfo.QueryTypeFieldInfo(FileDescription.IgnoreUnknownColumns, i); if (tfi == null) { continue; } if (FileDescription.EnforceCsvColumnAttribute && (!tfi.HasColumnAttribute)) { // enforcing column attr, but this field/prop has no column attr. // So there are too many fields in this record. throw new TooManyNonCsvColumnDataFieldsException(typeof(T).ToString(), row[i].LineNbr, FileName); } if ((!FileDescription.FirstLineHasColumnNames) && (tfi.Index == ColumnAttribute.McDefaultFieldIndex)) { // First line in the file does not have field names, so we're // depending on the FieldIndex of each field in the type // to ensure each value is placed in the correct field. // However, now hit a field where there is no FieldIndex. throw new MissingFieldIndexException(typeof(T).ToString(), row[i].LineNbr, FileName); } if (FileDescription.UseFieldIndexForReadingData && (!FileDescription.FirstLineHasColumnNames) && (tfi.Index > row.Count)) { throw new WrongFieldIndexException(typeof(T).ToString(), row[i].LineNbr, FileName); } var index = FileDescription.UseFieldIndexForReadingData ? tfi.Index - 1 : i; // value to put in the object var value = row[index].Value; if (string.IsNullOrEmpty(value) && tfi.TypeName != "String") { if (!tfi.CanBeNull) { throw new MissingRequiredFieldException( typeof(T).ToString(), tfi.Name, row[i].LineNbr, FileName); } } else { if (tfi.OutputFormat == "HH:mm:ss.fff") { if (value != null) { value = value.Substring(0, 8) + "." + value.Substring(9); } } var objValue = tfi.UpdateObjectValue(value, FileDescription.FileCultureInfo); var info = tfi.MemberInfo as PropertyInfo; if (info != null) { info.SetValue(obj, objValue, null); } else { ((FieldInfo)tfi.MemberInfo).SetValue(obj, objValue); } } } for (var i = row.Count; i < fieldIndexInfo.IndexToInfo.Length; i++) { var tfi = fieldIndexInfo.IndexToInfo[i]; if (((!FileDescription.EnforceCsvColumnAttribute) || tfi.HasColumnAttribute) && (!tfi.CanBeNull)) { ae.AddException(new MissingRequiredFieldException(typeof(T).ToString(), tfi.Name, row[row.Count - 1].LineNbr, FileName)); } } return(obj); }
private IEnumerable <T> ReadData <T>( string fileName, Stream stream, String sheetName, ExcelFileDescription fileDescription) where T : class, new() { // If T implements IDataRow, then we're reading raw data rows bool readingRawDataRows = typeof(IDataRow).IsAssignableFrom(typeof(T)); // The constructor for FieldMapper_Reading will throw an exception if there is something // wrong with type T. So invoke that constructor before you open the file, because if there // is an exception, the file will not be closed. // // If T implements IDataRow, there is no need for a FieldMapper, because in that case we're returning // raw data rows. FieldMapper_Reading <T> fm = null; if (!readingRawDataRows) { fm = new FieldMapper_Reading <T>(fileDescription, fileName, false); } // ------- // Each time the IEnumerable<T> that is returned from this method is // accessed in a foreach, ReadData is called again (not the original Read overload!) // // So, open the file here, or rewind the stream. bool readingFile = !string.IsNullOrEmpty(fileName); if (readingFile) { stream = File.Open(fileName, FileMode.Open, FileAccess.Read); } else { // Rewind the stream if ((stream == null) || (!stream.CanSeek)) { throw new BadStreamException(); } } ExcelStream es = new ExcelStream(stream, null, sheetName); // If we're reading raw data rows, instantiate a T so we return objects // of the type specified by the caller. // Otherwise, instantiate a DataRow, which also implements IDataRow. 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 { Dictionary <int, int> charLengths = null; if (!readingRawDataRows) { charLengths = fm.GetCharLengths(); } bool firstRow = true; while (es.ReadRow(row, charLengths)) { // Skip empty lines. // Important. If there is a newline at the end of the last data line, the code // thinks there is an empty line after that last data line. 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) { // Seeing that the AggregatedException was thrown, maximum number of exceptions // must have been reached, so rethrow. // Catch here, so you don't add an AggregatedException to an AggregatedException throw ae2; } catch (Exception e) { // Store the exception in the AggregatedException ae. // That way, if a file has many errors leading to exceptions, // you get them all in one go, packaged in a single aggregated exception. ae.AddException(e); } yield return(obj); } firstRow = false; } } finally { if (readingFile) { stream.Close(); } // If any exceptions were raised while reading the data from the file, // they will have been stored in the AggregatedException ae. // In that case, time to throw ae. ae.ThrowIfExceptionsStored(); } }
/// /////////////////////////////////////////////////////////////////////// /// ReadObject /// /// <summary> /// Creates an object of type T from the data in row and returns that object. /// /// </summary> /// <param name="row"></param> /// <param name="firstRow"></param> /// <returns></returns> public T ReadObject(IDataRow row, AggregatedException ae) { //If there are more columns than the required if (row.Count > m_IndexToInfo.Length) { //Are we ignoring unknown columns? if (!m_fileDescription.IgnoreUnknownColumns) { // Too many fields throw new TooManyDataFieldsException(typeof(T).ToString(), row[0].LineNbr, m_fileName); } } // ----- 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); for (int i = 0; i < maxRowCount; i++) { TypeFieldInfo tfi; //If there is some index mapping generated and the IgnoreUnknownColums is `true` if (m_fileDescription.IgnoreUnknownColumns && _mappingIndexes.Count > 0) { if (!_mappingIndexes.ContainsKey(i)) { continue; } tfi = m_IndexToInfo[_mappingIndexes[i]]; } else { tfi = m_IndexToInfo[i]; } if (m_fileDescription.EnforceCsvColumnAttribute && (!tfi.hasColumnAttribute)) { // enforcing column attr, but this field/prop has no column attr. // So there are too many fields in this record. throw new TooManyNonCsvColumnDataFieldsException(typeof(T).ToString(), row[i].LineNbr, m_fileName); } // ----- if ((!m_fileDescription.FirstLineHasColumnNames) && (tfi.index == Int32.MaxValue)) { // First line in the file does not have field names, so we're // depending on the FieldIndex of each field in the type // to ensure each value is placed in the correct field. // However, now hit a field where there is no FieldIndex. throw new MissingFieldIndexException(typeof(T).ToString(), row[i].LineNbr, m_fileName); } // ----- if (m_fileDescription.UseFieldIndexForReadingData && (!m_fileDescription.FirstLineHasColumnNames) && (tfi.index > row.Count)) { // First line in the file does not have field names, so we're // depending on the FieldIndex of each field in the type // to ensure each value is placed in the correct field. // However, now hit a field where the FieldIndex is bigger // than the total number of items in a row generated by the separatorChar throw new WrongFieldIndexException(typeof(T).ToString(), row[i].LineNbr, m_fileName); } int index = m_fileDescription.UseFieldIndexForReadingData ? tfi.index - 1 : i; // value to put in the object string value = row[index].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; // Normally, either tfi.typeConverter is not null, // or tfi.parseNumberMethod is not null. // if (tfi.typeConverter != null) { objValue = tfi.typeConverter.ConvertFromString( null, m_fileDescription.FileCultureInfo, value); } else if (tfi.parseExactMethod != null) { objValue = tfi.parseExactMethod.Invoke( tfi.fieldType, new Object[] { value, tfi.outputFormat, m_fileDescription.FileCultureInfo }); } else if (tfi.parseNumberMethod != null) { objValue = tfi.parseNumberMethod.Invoke( tfi.fieldType, new Object[] { value, tfi.inputNumberStyle, m_fileDescription.FileCultureInfo }); } else { // No TypeConverter and no Parse method available. // Try direct approach. 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); } } } // Visit any remaining fields in the type for which no value was given // in the data row, to see whether any of those was required. // 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++) { 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); }
public T ReadObject(IDataRow row, AggregatedException ae) { if (row.Count > fieldIndexInfo.IndexToInfo.Length) { //Are we ignoring unknown columns? if (!FileDescription.IgnoreUnknownColumns) { // Too many fields throw new TooManyDataFieldsException(typeof(T).ToString(), row[0].LineNbr, FileName); } } T obj = new T(); //If we will be using the mappings, we just iterate through all the cells in this row int maxRowCount = fieldIndexInfo.GetMaxRowCount(row.Count); for (int i = 0; i < maxRowCount; i++) { TypeFieldInfo tfi = fieldIndexInfo.QueryTypeFieldInfo(FileDescription.IgnoreUnknownColumns, i); if (tfi == null) { continue; } if (FileDescription.EnforceCsvColumnAttribute && (!tfi.HasColumnAttribute)) { // enforcing column attr, but this field/prop has no column attr. // So there are too many fields in this record. throw new TooManyNonCsvColumnDataFieldsException(typeof(T).ToString(), row[i].LineNbr, FileName); } if ((!FileDescription.FirstLineHasColumnNames) && (tfi.Index == ColumnAttribute.McDefaultFieldIndex)) { // First line in the file does not have field names, so we're // depending on the FieldIndex of each field in the type // to ensure each value is placed in the correct field. // However, now hit a field where there is no FieldIndex. throw new MissingFieldIndexException(typeof(T).ToString(), row[i].LineNbr, FileName); } if (FileDescription.UseFieldIndexForReadingData && (!FileDescription.FirstLineHasColumnNames) && (tfi.Index > row.Count)) { // First line in the file does not have field names, so we're // depending on the FieldIndex of each field in the type // to ensure each value is placed in the correct field. // However, now hit a field where the FieldIndex is bigger // than the total number of items in a row generated by the separatorChar throw new WrongFieldIndexException(typeof(T).ToString(), row[i].LineNbr, FileName); } int index = FileDescription.UseFieldIndexForReadingData ? tfi.Index - 1 : i; // value to put in the object string value = row[index].Value; if (value == null) { if (!tfi.CanBeNull) { ae.AddException(new MissingRequiredFieldException( typeof(T).ToString(), tfi.Name, row[i].LineNbr, FileName)); } } else { try { if (tfi.OutputFormat == "HH:mm:ss.fff") { value = value.Substring(0, 8) + "." + value.Substring(9); } Object objValue = tfi.UpdateObjectValue(value, FileDescription.FileCultureInfo); 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, FileName, e); } ae.AddException(e); } } } // Visit any remaining fields in the type for which no value was given // in the data row, to see whether any of those was required. // If only looking at fields with CsvColumn attribute, do ignore // fields that don't have that attribute. for (int i = row.Count; i < fieldIndexInfo.IndexToInfo.Length; i++) { TypeFieldInfo tfi = fieldIndexInfo.IndexToInfo[i]; if (((!FileDescription.EnforceCsvColumnAttribute) || tfi.HasColumnAttribute) && (!tfi.CanBeNull)) { ae.AddException(new MissingRequiredFieldException(typeof(T).ToString(), tfi.Name, row[row.Count - 1].LineNbr, FileName)); } } return(obj); }
public RowReader(CsvFileDescription description, AggregatedException ae) { Obj = default(T); _description = description; _ae = ae; }