Skip to content

Instantly share code, notes, and snippets.

@timhobbs
Created May 24, 2013 19:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timhobbs/5646024 to your computer and use it in GitHub Desktop.
Save timhobbs/5646024 to your computer and use it in GitHub Desktop.
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