using System;
using System.Data;

public class NHibernateHelper
{
    
/**//// <summary>
    /// Ilist<T> 转换成 DataSet
    /// </summary>
    /// <param name="list"></param>
    /// <returns></returns>
    public static DataSet ConvertToDataSet<T>(IList<T> list)
    {
        
if (list == null || list.Count <= 0)
        {
            
return null;
        }

        DataSet ds 
= new DataSet();
        DataTable dt 
= new DataTable(typeof(T).Name);
        DataColumn column;
        DataRow row;

        System.Reflection.PropertyInfo[] myPropertyInfo 
= typeof(T).GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance);

        foreach (T t 
in list)
        {
            
if (t == null)
            {
                
continue;
            }

            row 
= dt.NewRow();

            
for (int i = 0, j = myPropertyInfo.Length; i < j; i++)
            {
                System.Reflection.PropertyInfo pi 
= myPropertyInfo[i];

                string name 
= pi.Name;

                
if (dt.Columns[name] == null)
                {
                    column 
= new DataColumn(name, pi.PropertyType);
                    dt.Columns.Add(column);
                }

                row[name] 
= pi.GetValue(t, null);
            }

            dt.Rows.Add(row);
        }

        ds.Tables.Add(dt);

        
return ds;
    }

}
支持C#2.0。

相关文章:

  • 2022-02-20
  • 2022-03-04
  • 2021-10-05
  • 2021-06-26
  • 2021-12-21
  • 2022-12-23
  • 2021-08-06
  • 2022-02-27
猜你喜欢
  • 2021-10-15
  • 2021-11-10
  • 2021-09-07
  • 2021-08-21
  • 2022-12-23
相关资源
相似解决方案