Instantly share code, notes, and snippets.

Embed
What would you like to do?
Converts CSV file contents (as IList<string>) to a typed IList<T>
private IList<T> ConvertCsvToList<T>(IList<string> csv, string[] header) {
var list = new List<T>();
foreach (var row in csv) {
var columns = row.Split(',');
T obj = (T)Activator.CreateInstance(typeof(T));
for (int i = 0; i < columns.Length; i++) {
var h = Regex.Match(header[i].Replace("@", "_"), @"(?<="")(?:\\.|[^""\\])*(?="")").Value;
var c = Regex.Match(columns[i], @"(?<="")(?:\\.|[^""\\])*(?="")").Value;
var prop = typeof(Em.Schools.Data.Domain.Match).GetProperty(h);
if (prop.PropertyType == typeof(int)) {
prop.SetValue(obj, Convert.ToInt32(c), null);
} else {
prop.SetValue(obj, c, null);
}
}
list.Add(obj);
}
return list;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment