|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Data; |
| 4 | +using System.IO; |
| 5 | +using System.Linq; |
| 6 | +using System.Reflection; |
| 7 | + |
| 8 | +namespace NTestDataBuilder.DataSources.FileData |
| 9 | +{ |
| 10 | + public static class DataTableExtensions |
| 11 | + { |
| 12 | + public static IEnumerable<T> AsEnumerable<T>(this DataTable table) where T : new() |
| 13 | + { |
| 14 | + if (table == null) |
| 15 | + throw new NullReferenceException("DataTable"); |
| 16 | + int propertiesLength = typeof(T).GetProperties().Length; |
| 17 | + if (propertiesLength == 0) |
| 18 | + throw new NullReferenceException("Properties"); |
| 19 | + var objList = new List<T>(); |
| 20 | + |
| 21 | + foreach (DataRow row in table.Rows) |
| 22 | + { |
| 23 | + var obj = new T(); |
| 24 | + PropertyInfo[] objProperties = obj.GetType().GetProperties(); |
| 25 | + for (int i = 0; i < propertiesLength; i++) |
| 26 | + { |
| 27 | + PropertyInfo property = objProperties[i]; |
| 28 | + if (table.Columns.Contains(property.Name)) |
| 29 | + { |
| 30 | + object objValue = row[property.Name]; |
| 31 | + var propertyType = property.PropertyType; |
| 32 | + if (propertyType.IsGenericType && propertyType.GetGenericTypeDefinition() == typeof(Nullable<>)) |
| 33 | + propertyType = propertyType.GetGenericArguments()[0]; |
| 34 | + objProperties[i].SetValue(obj, Convert.ChangeType(objValue, propertyType, System.Globalization.CultureInfo.CurrentCulture), null); |
| 35 | + } |
| 36 | + } |
| 37 | + objList.Add(obj); |
| 38 | + } |
| 39 | + |
| 40 | + return objList; |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + public class Person |
| 45 | + { |
| 46 | + public string FirstName { get; set; } |
| 47 | + public string LastName { get; set; } |
| 48 | + public string CompanyName { get; set; } |
| 49 | + public string Address { get; set; } |
| 50 | + public string City { get; set; } |
| 51 | + public string County { get; set; } |
| 52 | + public string Postal { get; set; } |
| 53 | + public string Phone1 { get; set; } |
| 54 | + public string Phone2 { get; set; } |
| 55 | + public string Email { get; set; } |
| 56 | + public string Web { get; set; } |
| 57 | + } |
| 58 | + public class PersonData |
| 59 | + { |
| 60 | + public static IList<Person> People { get; private set; } |
| 61 | + |
| 62 | + static PersonData() |
| 63 | + { |
| 64 | + People = ConvertCSVtoDataTable() |
| 65 | + .AsEnumerable<Person>() |
| 66 | + .ToList(); |
| 67 | + } |
| 68 | + |
| 69 | + private static DataTable ConvertCSVtoDataTable() |
| 70 | + { |
| 71 | + var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("NTestDataBuilder.DataSources.FileData.uk-500.csv"); |
| 72 | + var sr = new StreamReader(stream); |
| 73 | + var headers = sr.ReadLine().Split(','); |
| 74 | + var dt = new DataTable(); |
| 75 | + foreach (string header in headers) |
| 76 | + { |
| 77 | + dt.Columns.Add(header); |
| 78 | + } |
| 79 | + while (!sr.EndOfStream) |
| 80 | + { |
| 81 | + var rows = sr.ReadLine().Split(','); |
| 82 | + var dr = dt.NewRow(); |
| 83 | + for (int i = 0; i < headers.Length; i++) |
| 84 | + { |
| 85 | + dr[i] = rows[i]; |
| 86 | + } |
| 87 | + dt.Rows.Add(dr); |
| 88 | + } |
| 89 | + return dt; |
| 90 | + } |
| 91 | + } |
| 92 | +} |
0 commit comments