欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

DataTables List互相转换的实现类示例

程序员文章站 2024-02-16 22:22:22
复制代码 代码如下:public static class datatablehelper    {   &nb...

复制代码 代码如下:

public static class datatablehelper
    {
        public static datatable convertto<t>(ilist<t> list)
        {
            datatable table = createtable<t>();
            type entitytype = typeof(t);
            propertydescriptorcollection properties = typedescriptor.getproperties(entitytype);
            foreach (t item in list)
            {
                datarow row = table.newrow();
                foreach (propertydescriptor prop in properties)
                    row[prop.name] = prop.getvalue(item);
                table.rows.add(row);
            }
            return table;
        }

        public static ilist<t> convertto<t>(ilist<datarow> rows)
        {
            ilist<t> list = null;
            if (rows != null)
            {
                list = new list<t>();
                foreach (datarow row in rows)
                {
                    t item = createitem<t>(row);
                    list.add(item);
                }
            }
            return list;
        }

        public static ilist<t> convertto<t>(datatable table)
        {
            if (table == null)
                return null;

            list<datarow> rows = new list<datarow>();
            foreach (datarow row in table.rows)
                rows.add(row);

            return convertto<t>(rows);
        }

        //convert datarow into t object
        public static t createitem<t>(datarow row)
        {
            string columnname;
            t obj = default(t);
            if (row != null)
            {
                obj = activator.createinstance<t>();
                foreach (datacolumn column in row.table.columns)
                {
                    columnname = column.columnname;
                    //get property with same columnname
                    propertyinfo prop = obj.gettype().getproperty(columnname);
                    try
                    {
                        //get value for the column
                        object value = (row[columnname].gettype() == typeof(dbnull))
                        ? null : row[columnname];
                        //set property value
                        if (prop.canwrite)    //判断其是否可写
                            prop.setvalue(obj, value, null);
                    }
                    catch
                    {
                        throw;
                        //catch whatever here
                    }
                }
            }
            return obj;
        }

        public static datatable createtable<t>()
        {
            type entitytype = typeof(t);
            datatable table = new datatable(entitytype.name);
            propertydescriptorcollection properties = typedescriptor.getproperties(entitytype);

            foreach (propertydescriptor prop in properties)
                table.columns.add(prop.name, prop.propertytype);

            return table;
        }
    }