private ICsvRowActivator <T> CreateSut <T>(string[] headerrow)
        {
            ICsvRowActivatorFactoryFactory <T> csvRowActivatorFactoryFactory
                = new DynamicCsvRowActivatorFactoryFactory <T>();
            IColumnNameMapper           columnNameMapper  = new DefaultColumnNameMapper();
            IConverterProvider          converterProvider = new DefaultConverterProvider();
            ICsvRowActivatorFactory <T> csvRowActivatorFactory
                = csvRowActivatorFactoryFactory.Create(converterProvider, columnNameMapper);
            ICsvRowActivator <T> activator = csvRowActivatorFactory.Create(headerrow);

            return(activator);
        }
예제 #2
0
        public IEnumerable <T> ReadAll()
        {
            // Create an activator factory
            ICsvRowActivatorFactory <T> activatorFactory = activatorFactoryFactory.Create(
                new DefaultConverterProvider(),
                new DefaultColumnNameMapper()
                );

            // Create a tokenizer that reads from the csvStream
            CsvRowTokenizer tokenizer = new CsvRowTokenizer(csvStream);

            // Start streaming data from the tokenizer
            IEnumerable <string[]> rows = tokenizer.ReadAllRows();

            // Grab the first line (the header)
            IEnumerator <string[]> enumerator = rows.GetEnumerator();

            if (!enumerator.MoveNext())
            {
                throw new InvalidDataException("No header found!");
            }
            string[] headerrow   = enumerator.Current;
            int      columnCount = headerrow.Length;

            // Start streaming tokenized data into the activator
            ICsvRowActivator <T> activator = activatorFactory.Create(headerrow);

            // Stream out all of the CSV rows as objects
            while (enumerator.MoveNext())
            {
                if (enumerator.Current.Length != columnCount)
                {
                    throw new InvalidDataException(
                              string.Format("Field count mismatch: a row had {0} field(s), but header had {1}!", enumerator.Current.Length, columnCount)
                              );
                }
                yield return(activator.CreateFromRow(enumerator.Current));
            }
        }