Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static class DataTableExtensions
- {
- private static Dictionary<Type,IList<PropertyInfo>> typeDictionary = new Dictionary<Type, IList<PropertyInfo>>();
- public static IList<PropertyInfo> GetPropertiesForType<T>()
- {
- var type = typeof(T);
- if(!typeDictionary.ContainsKey(typeof(T))
- {
- typeDictionary.Add(type, type.GetProperties().ToList());
- }
- return typeDictionary[type];
- }
- public static IList<T> ToList<T>(this DataTable table) where T : new()
- {
- IList<PropertyInfo> properties = GetPropertiesForType<T>();
- IList<T> result = new List<T>();
- foreach (var row in table.Rows)
- {
- var item = CreateItemFromRow<T>((DataRow)row, properties);
- result.Add(item);
- }
- return result;
- }
- private static T CreateItemFromRow<T>(DataRow row, IList<PropertyInfo> properties) where T : new()
- {
- T item = new T();
- foreach (var property in properties)
- {
- property.SetValue(item, row[property.Name], null);
- }
- return item;
- }
- }
Add Comment
Please, Sign In to add comment