public static List <object> DeserializeData(List <List <string> > parsedCsv, CsvDataDescriptor descriptor, int headerRow = 0, CsvParser.DelimiterType delimiter = CsvParser.DelimiterType.Auto, IEnumerable <Type> customTypes = null) { if (descriptor == null) { if (customTypes == null) { throw new NullReferenceException("descriptor for generic import can't be null, you must at least specify types!"); } descriptor = CsvDataDescriptor.CreateDescriptor(customTypes, parsedCsv[headerRow]); } if (descriptor.ItemTypeColumn == -1) { throw new ArgumentException("descriptor for generic import must have type column set!"); } List <object> list = new List <object>(); for (int i = headerRow + 1; i < parsedCsv.Count; i++) { List <string> list2 = parsedCsv[i]; if (list2.Count >= descriptor.ItemTypeColumn + 1) { string text = list2[descriptor.ItemTypeColumn]; if (!string.IsNullOrEmpty(text)) { Type itemType = descriptor.GetItemType(text); if (itemType == null) { Debug.LogWarning("Can't instantiate type " + text + " this type is not in the descriptor!"); } else { object obj = Activator.CreateInstance(itemType); CsvSerializer.Deserialize(obj, list2, descriptor); list.Add(obj); } } } } return(list); }
public static List <T> DeserializeData <T>(List <List <string> > parsedCsv, CsvDataDescriptor descriptor = null, int headerRow = 0) where T : new() { if (headerRow >= parsedCsv.Count) { throw new ArgumentException("Header row index bigger then data!", "headerRow"); } if (descriptor == null) { descriptor = CsvDataDescriptor.CreateDescriptor(typeof(T), parsedCsv[headerRow]); } List <T> list = new List <T>(parsedCsv.Count - headerRow - 1); for (int i = headerRow + 1; i < parsedCsv.Count; i++) { List <string> list2 = parsedCsv[i]; if (list2.Count != 0) { T t = (default(T) == null) ? Activator.CreateInstance <T>() : default(T); CsvSerializer.Deserialize(t, list2, descriptor); list.Add(t); } } return(list); }