/// <summary>
        ///     Reads a record from the CSV file.
        /// </summary>
        /// <returns>
        ///     A <see cref="T:System.Collections.Generic.List`1" /> of fields for the record read.
        ///     If there are no more records, null is returned.
        /// </returns>
        public virtual string[] Read()
        {
            string[] strArrays;
            CheckDisposed();
            try
            {
                if (configuration.HasExcelSeparator && !hasExcelSeparatorBeenRead)
                {
                    ReadExcelSeparator();
                }
                var strArrays1 = ReadLine();
                if (configuration.DetectColumnCountChanges && strArrays1 != null && FieldCount > 0)
                {
                    if (FieldCount == strArrays1.Length)
                    {
                        if (!strArrays1.Any(field => field == null))
                        {
                            goto Label0;
                        }
                    }
                    throw new CsvBadDataException("An inconsistent number of columns has been detected.");
                }
Label0:
                strArrays = strArrays1;
            }
            catch (Exception exception)
            {
                ExceptionHelper.AddExceptionDataMessage(exception, this, null, null, null, null);
                throw;
            }
            return(strArrays);
        }
Exemple #2
0
        /// <summary>
        /// Reads a record from the CSV file.
        /// </summary>
        /// <returns>A <see cref="List{String}" /> of fields for the record read.
        /// If there are no more records, null is returned.</returns>
        public virtual string[] Read()
        {
            CheckDisposed();

            try
            {
                var row = ReadLine();

                if (configuration.DetectColumnCountChanges && row != null)
                {
                    if (columnCount > 0 && (columnCount != row.Length ||
#if NET_2_0
                                            EnumerableHelper.Any(row, field => field == null)
#else
                                            row.Any(field => field == null)
#endif
                                            ))
                    {
                        throw new CsvBadDataException("An inconsistent number of columns has been detected.");
                    }
                    columnCount = row.Length;
                }

                return(row);
            }
            catch (Exception ex)
            {
                ExceptionHelper.AddExceptionDataMessage(ex, this, null, null, null, null);
                throw;
            }
        }
Exemple #3
0
        /// <summary>
        /// Reads a record from the CSV file.
        /// </summary>
        /// <returns>A <see cref="List{String}" /> of fields for the record read.
        /// If there are no more records, null is returned.</returns>
        public virtual string[] Read()
        {
            CheckDisposed();

            try
            {
                if (configuration.HasExcelSeparator && !hasExcelSeparatorBeenRead)
                {
                    ReadExcelSeparator();
                }

                var row = ReadLine();

                if (configuration.DetectColumnCountChanges && row != null)
                {
                    if (FieldCount > 0 && (FieldCount != row.Length || row.Any(field => field == null)))
                    {
                        throw new CsvBadDataException("An inconsistent number of columns has been detected.");
                    }
                }

                return(row);
            }
            catch (Exception ex)
            {
                ExceptionHelper.AddExceptionDataMessage(ex, this, null, null, null, null);
                throw;
            }
        }
Exemple #4
0
        /// <summary>
        /// Writes the list of records to the CSV file.
        /// </summary>
        /// <param name="records">The list of records to write.</param>
        public virtual void WriteRecords(IEnumerable records)
        {
            CheckDisposed();

            foreach (var record in records)
            {
                if (configuration.HasHeaderRecord && !hasHeaderBeenWritten &&
#if !WINRT_4_5
                    !record.GetType().IsPrimitive
#else
                    !record.GetType().GetTypeInfo().IsPrimitive
#endif
                    )
                {
                    WriteHeader(record.GetType());
                }

                try
                {
                    GetWriteRecordAction(record.GetType()).DynamicInvoke(record);
                }
                catch (Exception ex)
                {
                    ExceptionHelper.AddExceptionDataMessage(ex, null, record.GetType(), null, null, null);
                    throw;
                }

                NextRecord();
            }
        }
Exemple #5
0
        /// <summary>
        /// Writes the list of records to the CSV file.
        /// </summary>
        /// <param name="records">The list of records to write.</param>
        public virtual void WriteRecords(IEnumerable records)
        {
            CheckDisposed();

            if (configuration.HasExcelSeparator && !hasExcelSeperatorBeenRead)
            {
                WriteExcelSeparator();
                hasExcelSeperatorBeenRead = true;
            }

            foreach (var record in records)
            {
                if (configuration.HasHeaderRecord && !hasHeaderBeenWritten && !record.GetType().IsPrimitive)
                {
                    WriteHeader(record.GetType());
                }

                try
                {
                    GetWriteRecordAction(record.GetType()).DynamicInvoke(record);
                }
                catch (Exception ex)
                {
                    ExceptionHelper.AddExceptionDataMessage(ex, null, record.GetType(), null, null, null);
                    throw;
                }

                NextRecord();
            }
        }
Exemple #6
0
        /// <summary>
        /// Writes the list of records to the CSV file.
        /// </summary>
        /// <typeparam name="T">The type of the record.</typeparam>
        /// <param name="records">The list of records to write.</param>
        public virtual void WriteRecords <T>(IEnumerable <T> records)
        {
            CheckDisposed();

            if (configuration.HasHeaderRecord &&
#if !WINRT_4_5
                !typeof(T).IsPrimitive
#else
                !typeof(T).GetTypeInfo().IsPrimitive
#endif
                )
            {
                WriteHeader <T>();
            }

            foreach (var record in records)
            {
                try
                {
                    GetWriteRecordAction <T>()(record);
                }
                catch (Exception ex)
                {
                    ExceptionHelper.AddExceptionDataMessage(ex, null, typeof(T), null, null, null);
                    throw;
                }

                NextRecord();
            }
        }
Exemple #7
0
        /// <summary>
        /// Writes the list of records to the CSV file.
        /// </summary>
        /// <param name="type">The type of the record.</param>
        /// <param name="records">The list of records to write.</param>
        public virtual void WriteRecords(Type type, IEnumerable records)
        {
            CheckDisposed();

            if (configuration.HasHeaderRecord &&
#if !WINRT_4_5
                !type.IsPrimitive
#else
                !type.GetTypeInfo().IsPrimitive
#endif
                )
            {
                WriteHeader(type);
            }

            foreach (var record in records)
            {
                try
                {
                    GetWriteRecordAction(type).DynamicInvoke(record);
                }
                catch (Exception ex)
                {
                    ExceptionHelper.AddExceptionDataMessage(ex, null, type, null, null, null);
                    throw;
                }

                NextRecord();
            }
        }
Exemple #8
0
        /// <summary>
        /// Writes the record to the CSV file.
        /// </summary>
        /// <typeparam name="T">The type of the record.</typeparam>
        /// <param name="record">The record to write.</param>
        public virtual void WriteRecord <T>(T record)
        {
            CheckDisposed();

#if !NET_2_0 && !NET_3_5 && !PCL
            var dynamicRecord = record as IDynamicMetaObjectProvider;
            if (dynamicRecord != null)
            {
                if (configuration.HasHeaderRecord && !hasHeaderBeenWritten)
                {
                    WriteDynamicHeader(dynamicRecord);
                }

                if (!typeActions.ContainsKey(dynamicRecord.GetType()))
                {
                    CreateActionForDynamic(dynamicRecord);
                }
            }
#endif

            try
            {
                GetWriteRecordAction <T>()(record);
                hasRecordBeenWritten = true;
                NextRecord();
            }
            catch (Exception ex)
            {
                ExceptionHelper.AddExceptionDataMessage(ex, null, record.GetType(), null, null, null);
                throw;
            }
        }
        /// <summary>
        /// Writes the list of records to the CSV file.
        /// </summary>
        /// <param name="records">The list of records to write.</param>
        public virtual void WriteRecords(IEnumerable records)
        {
            CheckDisposed();

            Type recordType = null;

            try
            {
                if (configuration.HasExcelSeparator && !hasExcelSeperatorBeenRead)
                {
                    WriteExcelSeparator();
                    hasExcelSeperatorBeenRead = true;
                }

                // Write the header. If records is a List<dynamic>, the header won't be written.
                // This is because typeof( T ) = Object.
                var genericEnumerable = records.GetType().GetInterfaces().FirstOrDefault(t => t.GetTypeInfo().IsGenericType&& t.GetGenericTypeDefinition() == typeof(IEnumerable <>));
                if (genericEnumerable != null)
                {
                    recordType = genericEnumerable.GetGenericArguments().Single();
                    var isPrimitive = recordType.GetTypeInfo().IsPrimitive;
                    if (configuration.HasHeaderRecord && !hasHeaderBeenWritten && !isPrimitive)
                    {
                        WriteHeader(recordType);
                    }
                }

                foreach (var record in records)
                {
                    // If records is a List<dynamic>, the header hasn't been written yet.
                    // Write the header based on the record type.
                    recordType = record.GetType();
                    var isPrimitive = recordType.GetTypeInfo().IsPrimitive;
                    if (configuration.HasHeaderRecord && !hasHeaderBeenWritten && !isPrimitive)
                    {
                        WriteHeader(recordType);
                    }

                    try
                    {
                        GetWriteRecordAction(record.GetType()).DynamicInvoke(record);
                    }
                    catch (TargetInvocationException ex)
                    {
                        throw ex.InnerException;
                    }

                    NextRecord();
                }
            }
            catch (Exception ex)
            {
                ExceptionHelper.AddExceptionDataMessage(ex, null, recordType, null, null, null);
                throw;
            }
        }
Exemple #10
0
        /// <summary>
        ///     Writes the list of records to the CSV file.
        /// </summary>
        /// <param name="records">The list of records to write.</param>
        public virtual void WriteRecords(IEnumerable records)
        {
            CheckDisposed();
            Type type = null;

            try
            {
                if (configuration.HasExcelSeparator && !hasExcelSeperatorBeenRead)
                {
                    WriteExcelSeparator();
                    hasExcelSeperatorBeenRead = true;
                }
                var type1 = records.GetType().GetInterfaces().FirstOrDefault(t =>
                {
                    if (!t.GetTypeInfo().IsGenericType)
                    {
                        return(false);
                    }
                    return(t.GetGenericTypeDefinition() == typeof(IEnumerable <>));
                });
                if (type1 != null)
                {
                    type = type1.GetGenericArguments().Single();
                    bool isPrimitive = type.GetTypeInfo().IsPrimitive;
                    if (configuration.HasHeaderRecord && !hasHeaderBeenWritten && !isPrimitive)
                    {
                        WriteHeader(type);
                    }
                }
                foreach (var record in records)
                {
                    type = record.GetType();
                    bool flag = type.GetTypeInfo().IsPrimitive;
                    if (configuration.HasHeaderRecord && !hasHeaderBeenWritten && !flag)
                    {
                        WriteHeader(type);
                    }
                    try
                    {
                        GetWriteRecordAction(record.GetType()).DynamicInvoke(record);
                    }
                    catch (TargetInvocationException targetInvocationException)
                    {
                        throw targetInvocationException.InnerException;
                    }
                    NextRecord();
                }
            }
            catch (Exception exception)
            {
                ExceptionHelper.AddExceptionDataMessage(exception, null, type, null, null, null);
                throw;
            }
        }
Exemple #11
0
        public virtual void WriteRecord(Type type, object record)
        {
            CheckDisposed();

            try
            {
                GetWriteRecordAction(type).DynamicInvoke(record);
                hasRecordBeenWritten = true;
                NextRecord();
            }
            catch (Exception ex)
            {
                ExceptionHelper.AddExceptionDataMessage(ex, null, type, null, null, null);
                throw;
            }
        }
Exemple #12
0
        /// <summary>
        /// Writes the record to the CSV file.
        /// </summary>
        /// <typeparam name="T">The type of the record.</typeparam>
        /// <param name="record">The record to write.</param>
        public virtual void WriteRecord <T>(T record)
        {
            CheckDisposed();

            try
            {
                GetWriteRecordAction <T>()(record);
                hasRecordBeenWritten = true;
                NextRecord();
            }
            catch (Exception ex)
            {
                ExceptionHelper.AddExceptionDataMessage(ex, null, record.GetType(), null, null, null);
                throw;
            }
        }
Exemple #13
0
        /// <summary>
        /// Gets the record.
        /// </summary>
        /// <param name="type">The <see cref="Type"/> of the record.</param>
        /// <returns>The record.</returns>
        public virtual object GetRecord(Type type)
        {
            CheckDisposed();
            CheckHasBeenRead();

            object record;

            try
            {
                record = GetReadRecordFunc(type).DynamicInvoke();
            }
            catch (Exception ex)
            {
                ExceptionHelper.AddExceptionDataMessage(ex, parser, type, namedIndexes, currentIndex, currentRecord);
                throw;
            }
            return(record);
        }
Exemple #14
0
        /// <summary>
        /// Writes the record to the CSV file.
        /// </summary>
        /// <typeparam name="T">The type of the record.</typeparam>
        /// <param name="record">The record to write.</param>
        public virtual void WriteRecord <T>(T record) where T : class
        {
            CheckDisposed();

            try
            {
                GetWriteRecordAction <T>()(record);
            }
            catch (Exception ex)
            {
                ExceptionHelper.AddExceptionDataMessage(ex, null, typeof(T), null, null, null);
                throw;
            }

            hasRecordBeenWritten = true;

            NextRecord();
        }
Exemple #15
0
        /// <summary>
        /// Gets the record converted into <see cref="Type"/> T.
        /// </summary>
        /// <typeparam name="T">The <see cref="Type"/> of the record.</typeparam>
        /// <returns>The record converted to <see cref="Type"/> T.</returns>
        public virtual T GetRecord <T>() where T : class
        {
            CheckDisposed();
            CheckHasBeenRead();

            T record;

            try
            {
                record = GetReadRecordFunc <T>()();
            }
            catch (Exception ex)
            {
                ExceptionHelper.AddExceptionDataMessage(ex, parser, typeof(T), namedIndexes, currentIndex, currentRecord);
                throw;
            }
            return(record);
        }
Exemple #16
0
        /// <summary>
        /// Writes the list of records to the CSV file.
        /// </summary>
        /// <param name="records">The list of records to write.</param>
        public virtual void WriteRecords(IEnumerable records)
        {
            CheckDisposed();

            if (configuration.HasExcelSeparator && !hasExcelSeperatorBeenRead)
            {
                WriteExcelSeparator();
                hasExcelSeperatorBeenRead = true;
            }

            // Write the header. If records is a List<dynamic>, the header won't be written.
            // This is because typeof( T ) = Object.
            if (records.GetType().IsGenericType)
            {
                var type = records.GetType().GetGenericArguments().First();
                if (configuration.HasHeaderRecord && !hasHeaderBeenWritten && !type.IsPrimitive)
                {
                    WriteHeader(type);
                }
            }

            foreach (var record in records)
            {
                // If records is a List<dynamic>, the header hasn't been written yet.
                // Write the header based on the record type.
                var type = record.GetType();
                if (configuration.HasHeaderRecord && !hasHeaderBeenWritten && !type.IsPrimitive)
                {
                    WriteHeader(type);
                }

                try
                {
                    GetWriteRecordAction(record.GetType()).DynamicInvoke(record);
                }
                catch (Exception ex)
                {
                    ExceptionHelper.AddExceptionDataMessage(ex, null, record.GetType(), null, null, null);
                    throw;
                }

                NextRecord();
            }
        }
Exemple #17
0
        /// <summary>
        /// Ignore no of line(s) in reader
        /// </summary>
        /// <param name="noOfLine"></param>
        /// <returns>string in ignored line(s)</returns>
        public virtual string IgnoreLines(int noOfLine)
        {
            CheckDisposed();

            try
            {
                string myLines = string.Empty;
                for (int i = 1; i > noOfLine - 1; i++)
                {
                    myLines += reader.ReadLine();
                }

                return(myLines);
            }
            catch (Exception ex)
            {
                ExceptionHelper.AddExceptionDataMessage(ex, this, null, null, null, null);
                throw;
            }
        }
Exemple #18
0
        /// <summary>
        /// Gets all the records in the CSV file and
        /// converts each to <see cref="Type"/> T. The Read method
        /// should not be used when using this.
        /// </summary>
        /// <param name="type">The <see cref="Type"/> of the record.</param>
        /// <returns>An <see cref="IList{Object}" /> of records.</returns>
        public virtual IEnumerable <object> GetRecords(Type type)
        {
            CheckDisposed();
            // Don't need to check if it's been read
            // since we're doing the reading ourselves.

            while (Read())
            {
                object record;
                try
                {
                    record = GetReadRecordFunc(type).DynamicInvoke();
                }
                catch (Exception ex)
                {
                    ExceptionHelper.AddExceptionDataMessage(ex, parser, type, namedIndexes, currentIndex, currentRecord);
                    throw;
                }
                yield return(record);
            }
        }
Exemple #19
0
        /// <summary>
        /// Gets all the records in the CSV file and
        /// converts each to <see cref="Type"/> T. The Read method
        /// should not be used when using this.
        /// </summary>
        /// <typeparam name="T">The <see cref="Type"/> of the record.</typeparam>
        /// <returns>An <see cref="IList{T}" /> of records.</returns>
        public virtual IEnumerable <T> GetRecords <T>() where T : class
        {
            CheckDisposed();
            // Don't need to check if it's been read
            // since we're doing the reading ourselves.

            while (Read())
            {
                T record;
                try
                {
                    record = GetReadRecordFunc <T>()();
                }
                catch (Exception ex)
                {
                    ExceptionHelper.AddExceptionDataMessage(ex, parser, typeof(T), namedIndexes, currentIndex, currentRecord);
                    throw;
                }

                yield return(record);
            }
        }
Exemple #20
0
        /// <summary>
        /// Writes the list of records to the CSV file.
        /// </summary>
        /// <param name="type">The type of the record.</param>
        /// <param name="records">The list of records to write.</param>
        public virtual void WriteRecords(Type type, IEnumerable <object> records)
        {
            CheckDisposed();

            if (configuration.HasHeaderRecord)
            {
                WriteHeader(type);
            }

            foreach (var record in records)
            {
                try
                {
                    GetWriteRecordAction(type).DynamicInvoke(this, record);
                }
                catch (Exception ex)
                {
                    ExceptionHelper.AddExceptionDataMessage(ex, null, type, null, null, null);
                    throw;
                }
                NextRecord();
            }
        }
Exemple #21
0
        /// <summary>
        /// Writes the list of records to the CSV file.
        /// </summary>
        /// <typeparam name="T">The type of the record.</typeparam>
        /// <param name="records">The list of records to write.</param>
        public virtual void WriteRecords <T>(IEnumerable <T> records)
        {
            CheckDisposed();

            if (configuration.HasHeaderRecord)
            {
                WriteHeader <T>();
            }

            foreach (var record in records)
            {
                try
                {
                    GetWriteRecordAction <T>()(record);
                }
                catch (Exception ex)
                {
                    ExceptionHelper.AddExceptionDataMessage(ex, null, typeof(T), null, null, null);
                    throw;
                }
                NextRecord();
            }
        }