123456789101112131415161718192021222324252627282930313233343536373839404142 |
- using ConsoleApp1.model;
- using CsvHelper.Configuration;
- using CsvHelper;
- using System;
- using System.Collections.Generic;
- using System.Globalization;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace ConsoleApp1.read
- {
- internal class ReadToCSV
- {
- public List<T> ImportToCsv<T>(string path)
- {
- List<T> data = new List<T>();
- using (StreamReader reader = new StreamReader(path))
- {
- string line;
- while ((line = reader.ReadLine()) != null)
- {
- string[] values = line.Split(',');
- T item = Activator.CreateInstance<T>();
- var properties = typeof(T).GetProperties();
- for (int i = 0; i < properties.Length; i++)
- {
- properties[i].SetValue(item, Convert.ChangeType(values[i], properties[i].PropertyType), null);
- }
- data.Add(item);
- }
- }
- return data;
- }
- }
- }
|