public void AddGetRemoveTest()
        {
            var customOptions = new TypeConverterOptions
            {
                Formats = new string[] { "custom" },
            };
            var typeConverterOptionsFactory = new TypeConverterOptionsCache();

            typeConverterOptionsFactory.AddOptions <string>(customOptions);
            var options = typeConverterOptionsFactory.GetOptions <string>();

            Assert.AreEqual(customOptions.Formats, options.Formats);

            typeConverterOptionsFactory.RemoveOptions <string>();

            options = typeConverterOptionsFactory.GetOptions <string>();

            Assert.AreNotEqual(customOptions.Formats, options.Formats);
        }
示例#2
0
        public void ParseFiles(out IEnumerable <EmployeeBankingInfo> employeeBankingInfo,
                               out IEnumerable <Payroll> payrollItems, string employeeBankingFileName = null, string payrollFileName = null)
        {
            TypeConverterOptionsCache y = new TypeConverterOptionsCache();

            y.GetOptions(typeof(decimal));
            using (var reader = new StreamReader(employeeBankingFileName ?? throw new ArgumentNullException(nameof(employeeBankingFileName))))
                using (var csv = new CsvReader(reader))
                {
                    csv.Configuration.RegisterClassMap <EmployeeBankingInfoMap>();
                    employeeBankingInfo = csv.GetRecords <EmployeeBankingInfo>().ToList();
                }
            using (var reader = new StreamReader(payrollFileName ?? throw new ArgumentNullException(nameof(payrollFileName))))
                using (var csv = new CsvReader(reader))
                {
                    csv.Configuration.RegisterClassMap <PayrollMap>();
                    payrollItems = csv.GetRecords <Payroll>().ToList();
                }
        }
示例#3
0
        public static List <Column> FetchData(string fileName)
        {
            using TextReader reader = File.OpenText(fileName);

            CsvReader csv = new CsvReader(reader, CultureInfo.InvariantCulture);

            csv.Configuration.Delimiter = ",";

            csv.Read();
            csv.ReadHeader();

            var index   = 0;
            var headers = new Dictionary <int, string>();

            while (csv.TryGetField(index, out string headerColumn))
            {
                headers.Add(index, headerColumn);
                index++;
            }

            var typeConverterOptions = new TypeConverterOptionsCache();

            typeConverterOptions.GetOptions(typeof(double)).NumberStyle = NumberStyles.Any;

            csv.Configuration.TypeConverterOptionsCache = typeConverterOptions;

            var columns = new Dictionary <int, List <string> >();

            while (csv.Read())
            {
                index = 0;
                while (csv.TryGetField(index, out string field))
                {
                    if (columns.ContainsKey(index))
                    {
                        columns[index].Add(field);
                    }
                    else
                    {
                        columns.Add(index, new List <string> {
                            field
                        });
                    }

                    index++;
                }
            }

            var invalidRecords = Enumerable.Range(0, headers.Count)
                                 .Where(i => columns.Any(c => string.IsNullOrEmpty(c.Value.ElementAt(i))));

            foreach (var column in columns)
            {
                foreach (var invalidRecord in invalidRecords)
                {
                    column.Value.RemoveAt(invalidRecord);
                }
            }

            var normalizedColumns = columns.Select((c, i) => Normalize(headers[i], c.Value));

            return(normalizedColumns.ToList());
        }