Created
May 24, 2013 19:44
-
-
Save timhobbs/5646024 to your computer and use it in GitHub Desktop.
Converts CSV file contents (as IList<string>) to a typed IList<T>
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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