public static T GetEntity<T>(DataTable table) where T : new()
    {
        T entity = new T();
        foreach (DataRow row in table.Rows)
        {
            foreach (var item in entity.GetType().GetProperties())
            {
                if (row.Table.Columns.Contains(item.Name))
                {
                    if (DBNull.Value != row[item.Name])
                    {
                        item.SetValue(entity, Convert.ChangeType(row[item.Name], item.PropertyType), null);
                    }

                }
            }
        }

        return entity;
    }

    public static IList<T> GetEntities<T>(DataTable table) where T : new()
    {
        IList<T> entities = new List<T>();
        foreach (DataRow row in table.Rows)
        {
            T entity = new T();
            foreach (var item in entity.GetType().GetProperties())
            {
                item.SetValue(entity, Convert.ChangeType(row[item.Name], item.PropertyType), null);
            }
            entities.Add(entity);
        }
        return entities;
    }

posted on 2012-05-29 13:31  Nature.j  阅读(7694)  评论(1编辑  收藏  举报