Esempio n. 1
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) where T : class
        {
            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();
            }
        }
Esempio n. 2
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 <T>(IEnumerable <T> records)
        {
            Type recordType = null;

            try
            {
                // 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 (context.WriterConfiguration.HasHeaderRecord && !context.HasHeaderBeenWritten && !isPrimitive && recordType != typeof(object))
                    {
                        WriteHeader(recordType);
                        if (context.HasHeaderBeenWritten)
                        {
                            NextRecord();
                        }
                    }
                }

                foreach (var record in records)
                {
                    recordType = record.GetType();

                    if (record is IDynamicMetaObjectProvider dynamicObject)
                    {
                        if (context.WriterConfiguration.HasHeaderRecord && !context.HasHeaderBeenWritten)
                        {
                            WriteDynamicHeader(dynamicObject);
                            NextRecord();
                        }
                    }
                    else
                    {
                        // If records is a List<dynamic>, the header hasn't been written yet.
                        // Write the header based on the record type.
                        var isPrimitive = recordType.GetTypeInfo().IsPrimitive;
                        if (context.WriterConfiguration.HasHeaderRecord && !context.HasHeaderBeenWritten && !isPrimitive)
                        {
                            WriteHeader(recordType);
                            NextRecord();
                        }
                    }

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

                    NextRecord();
                }
            }
            catch (Exception ex)
            {
                throw ex as CsvHelperException ?? new WriterException(context, "An unexpected error occurred.", ex);
            }
        }