CollectionsConvertor

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Reflection;
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
        }
    }
    public static class CollectionsConvertor
    {
        public static List DataTableToList(this DataTable dt) where T : class, new()
        {
            Type t = typeof(T);
            PropertyInfo[] propertys = t.GetProperties();
            List lst = new List();
            string typeName = string.Empty;
            foreach (DataRow dr in dt.Rows)
            {
                T entity = new T();
                foreach (PropertyInfo pi in propertys)
                {
                    typeName = pi.Name;
                    if (dt.Columns.Contains(typeName))
                    {
                        if (!pi.CanWrite) continue;
                        object value = dr[typeName];
                        if (value == DBNull.Value) continue;
                        if (pi.PropertyType == typeof(string))
                        {
                            pi.SetValue(entity, value.ToString(), null);
                        }
                        else if (pi.PropertyType == typeof(int) || pi.PropertyType == typeof(int?))
                        {
                            pi.SetValue(entity, int.Parse(value.ToString()), null);
                        }
                        else if (pi.PropertyType == typeof(DateTime?) || pi.PropertyType == typeof(DateTime))
                        {
                            pi.SetValue(entity, DateTime.Parse(value.ToString()), null);
                        }
                        else if (pi.PropertyType == typeof(float))
                        {
                            pi.SetValue(entity, float.Parse(value.ToString()), null);
                        }
                        else if (pi.PropertyType == typeof(double))
                        {
                            pi.SetValue(entity, double.Parse(value.ToString()), null);
                        }
                        else
                        {
                            pi.SetValue(entity, value, null);
                        }
                    }
                }
                lst.Add(entity);
            }
            return lst;
        }
        public static DataTable ListToDataTable(this IEnumerable value) where T : class,new()
        {
            List lstProperty = new List();
            Type type = typeof(T);
            DataTable dt = new DataTable();
            type.GetProperties().ToList().ForEach(p =>  //ForEach扩展方法,这里使用Array.ForEach(type.GetProperties(),p=>{})也是一样
            {
                lstProperty.Add(p);
                if (p.PropertyType.IsGenericType)//是否为泛型,泛型获取不到具体的类型
                {
                    dt.Columns.Add(p.Name);
                }
                else
                {
                    dt.Columns.Add(p.Name, p.PropertyType);
                }
            });
            if (value != null)
            {
                foreach (var item in value)
                {
                    //创建一个DataRow实例
                    DataRow row = dt.NewRow();
                    lstProperty.ForEach(p =>
                    {
                        row[p.Name] = p.GetValue(item, null);
                    });
                    dt.Rows.Add(row);
                }
            }
            return dt;
        }
        public static DataTable LinqToDataTable(IEnumerable varlist)
        {
            DataTable dataTable = new DataTable();
            // column names
            PropertyInfo[] propertyInfo = null;
            if (varlist == null) return dataTable;
            foreach (T t in varlist)
            {
                // Use reflection to get property names, to create table, Only first time, others
                if (propertyInfo == null)
                {
                    propertyInfo = ((Type)t.GetType()).GetProperties();
                    foreach (PropertyInfo pi in propertyInfo)
                    {
                        Type colType = pi.PropertyType;
                        if ((colType.IsGenericType) && (colType.GetGenericTypeDefinition()
                        == typeof(Nullable<>)))
                        {
                            colType = colType.GetGenericArguments()[0];
                        }
                        dataTable.Columns.Add(new DataColumn(pi.Name, colType));
                    }
                }
                DataRow dr = dataTable.NewRow();
                foreach (PropertyInfo pi in propertyInfo)
                {
                    dr[pi.Name] = pi.GetValue(t, null) == null ? DBNull.Value : pi.GetValue
                    (t, null);
                }
                dataTable.Rows.Add(dr);
            }
            return dataTable;
        }
        protected void btnShowDialogOne_Click(object sender, EventArgs e)
        {
            string key = "javascript";
            string param = string.Format("BoardCode={0}&BoardName={1}", "001", "zhuban");
            //Script. 标签不可省略;Js位于之前,即位于最后位置
            string script. = string.Format("", param);
            Page.ClientScript.RegisterStartupScript(this.GetType(), key, script);
        }
        protected void btnShowDialogTwo_Click(object sender, EventArgs e)
        {
            string key = "javascript";
            string param = string.Format("BoardCode={0}&BoardName={1}", "001", "zhuban");
            string script. = string.Format("OpenPage('ShowPage.aspx?{0}',600,500);",param);
            if (!Page.ClientScript.IsClientScriptBlockRegistered(key))
            {
                //Script直接写js,无需添加Script标签;Js位于Form标签之后,控件标签之前
                //Page.ClientScript.RegisterClientScriptBlock(this.GetType(), key, script, true);//Method A
                //ScriptManager.RegisterStartupScript(this, this.GetType(), "", script, true);//Method B
                //当无UpdatePanel时,Method A 和Method B相同,当有UpdatePanel时Method A失效;MethodB 和以下方法相同
                ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "", script, true);
            }
           
        }
    }
}

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/28699126/viewspace-772964/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/28699126/viewspace-772964/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值