DataTableをListに変換<br>の汎用クラス


コード:
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Reflection;

namespace NCL.Data
{
    /// <summary>
    ///  
    /// </summary>
    public class ModelConvertHelper<T> where  T : new()
    {
        public static IList<T> ConvertToModel(DataTable dt)
        {
            //  
            IList<T> ts = new List<T>();

            //  
            Type type = typeof(T);

            string tempName = "";

            foreach (DataRow dr in dt.Rows)
            {
                T t = new T();

                //  
                PropertyInfo[] propertys = t.GetType().GetProperties();

                foreach (PropertyInfo pi in propertys)
                {
                    tempName = pi.Name;

                    //  DataTable 
                    if (dt.Columns.Contains(tempName))
                    {
                        //  Setter
                        if (!pi.CanWrite) continue;

                        object value = dr[tempName];
                        if (value != DBNull.Value)
                            pi.SetValue(t, value, null);
                    }
                }

                ts.Add(t);
            }

            return ts;

        }
    }
}

使用方法:
//クエリー結果を取得DataTable dt=DbHelper.ExecuteDataTable(...);//DataTableをIListIListusers=ModelConvertHelperに変換する.ConvertToModel(dt);