首页  ·  知识 ·  编程语言
利用.NET反射机制实现IList到DataTable转换
佚名  http://hi.baidu.com/ldy201001/blog/i  .NET  编辑:dezai  图片来源:网络
using System;using System.Collections.Generic;using System.Text;using System.Data;using System.Collections;using System.Reflec

using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Collections;
using System.Reflection;

namespace KycBaseModule
...{
    public class KycFunction
    ...{
        public KycFunction() ...{ }
        /**////


        /// 实现对IList到DataSet的转换
        ///

        /// 待转换的IList
        /// 转换后的DataSet
        public static DataSet ListToDataSet(IList ResList)
        ...{
             DataSet RDS=new DataSet();
             DataTable TempDT = new DataTable();

            //此处遍历IList的结构并建立同样的DataTable
             System.Reflection.PropertyInfo[] p = ResList[0].GetType().GetProperties();
            foreach (System.Reflection.PropertyInfo pi in p)
            ...{
                 TempDT.Columns.Add(pi.Name,System.Type.GetType(pi.PropertyType.ToString()));
             }

            for (int i = 0; i < ResList.Count; i++)
            ...{
                 IList TempList = new ArrayList();
                //将IList中的一条记录写入ArrayList
                foreach (System.Reflection.PropertyInfo pi in p)
                ...{
                    object oo = pi.GetValue(ResList[i], null);
                     TempList.Add(oo);
                 }
               
                object[] itm=new object[p.Length];
                //遍历ArrayList向object[]里放数据
                for (int j = 0; j < TempList.Count; j++)
                ...{
                     itm.SetValue(TempList[j], j);
                 }
                //将object[]的内容放入DataTable
                     TempDT.LoadDataRow(itm, true);
             }
            //将DateTable放入DataSet
             RDS.Tables.Add(TempDT);
            //返回DataSet
            return RDS;
         }
     }
}

本文作者:佚名 来源:http://hi.baidu.com/ldy201001/blog/i
CIO之家 www.ciozj.com 微信公众号:imciow
   
免责声明:本站转载此文章旨在分享信息,不代表对其内容的完全认同。文章来源已尽可能注明,若涉及版权问题,请及时与我们联系,我们将积极配合处理。同时,我们无法对文章内容的真实性、准确性及完整性进行完全保证,对于因文章内容而产生的任何后果,本账号不承担法律责任。转载仅出于传播目的,读者应自行对内容进行核实与判断。请谨慎参考文章信息,一切责任由读者自行承担。
延伸阅读