-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCSVReader.cs
More file actions
87 lines (79 loc) · 2.71 KB
/
CSVReader.cs
File metadata and controls
87 lines (79 loc) · 2.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
namespace CSVLibrary
{
public class CSVReader<T> where T : class, new()
{
string _Path = "";
/// <summary>
///
/// </summary>
/// <param name = "path" > Path were the File should be fetched from INCLUDING.csv</param>
public CSVReader(string path)
{
_Path = path;
}
public T ReadSingle(char seperator = ';')
{
using (StreamReader sr = new StreamReader(_Path))
{
var newT = new T();
try
{
string[] daten = sr.ReadLine().Split(seperator);
foreach (var item in daten)
{
var infos = item.Split('=');
if (infos.Length == 3)
{
var PropertyName = infos[0];
var PropertyType = Type.GetType(infos[1]);
var PropertyValue = Convert.ChangeType(infos[2], PropertyType);
newT.GetType().GetProperty(PropertyName).SetValue(newT, PropertyValue);
}
}
return newT;
}
catch (Exception e)
{
throw e;
}
}
}
public List<T> ReadAll(char seperator = ';')
{
using (StreamReader sr = new StreamReader(_Path))
{
List<T> returnValue = new List<T>();
while (!sr.EndOfStream)
{
try
{
string[] daten = sr.ReadLine().Split(seperator);
var newT = new T();
foreach (var item in daten)
{
var infos = item.Split('=');
if (infos.Length == 3)
{
var PropertyName = infos[0];
var PropertyType = Type.GetType(infos[1]);
var PropertyValue = Convert.ChangeType(infos[2], PropertyType);
newT.GetType().GetProperty(PropertyName).SetValue(newT, PropertyValue);
}
}
returnValue.Add(newT);
}
catch (Exception e)
{
throw e;
}
}
return returnValue;
}
}
}
}