#   /// <summary>
#         /// DataSet装换为泛型集合
#         /// </summary>
#         /// <typeparam name="T"></typeparam>
#         /// <param name="p_DataSet">DataSet</param>
#         /// <param name="p_TableIndex"& gt;待转换数据表索引</param>
#         /// <returns></returns>
#         /// 2008-08-01 22:46 HPDV2806
#         public static IList<T> DataSetToIList<T>( DataSet p_DataSet, int p_TableIndex )
#         {
#             if ( p_DataSet == null || p_DataSet.Tables.Count < 0 )
#                 return null;
#             if ( p_TableIndex > p_DataSet.Tables.Count - 1 )
#                 return null;
#             if ( p_TableIndex < 0 )
#                 p_TableIndex = 0;
#
#             DataTable p_Data = p_DataSet.Tables[p_TableIndex];
#             // 返回值初始化
#             IList<T> result = new List<T>();
#             for ( int j = 0; j < p_Data.Rows.Count; j++ )
#             {
#                 T _t = (T)Activator.CreateInstance( typeof( T ) );
#                 PropertyInfo[] propertys = _t.GetType().GetProperties();
#                 foreach ( PropertyInfo pi in propertys )
#                 {
#                     for ( int i = 0; i < p_Data.Columns.Count; i++ )
#                     {
#                         // 属性与字段名称一致的进行赋值
#                         if ( pi.Name.Equals( p_Data.Columns[i].ColumnName ) )
#                         {
#                             // 数据库NULL值单独处理
#                             if ( p_Data.Rows[j][i] != DBNull.Value )
#                                 pi.SetValue( _t, p_Data.Rows[j][i], null );
#                             else
#                                 pi.SetValue( _t, null, null );
#                             break;
#                         }
#                     }
#                 }
#                 result.Add( _t );
#             }
#             return result;
#         }
===================================================================================
 T _t = (T)Activator.CreateInstance( typeof( T ) );这句话
error:No parameterless constructor defined for this object.
无参数构造函数定义为这个对象。查了好久    谢谢

解决方案 »

  1.   

    public IList <T> GetList <T>(DataTable table) 
            { 
                IList <T> list = new List <T>(); 
                T t = default(T); 
                PropertyInfo[] propertypes = null; 
                string tempName = string.Empty; 
                foreach (DataRow row in table.Rows) 
                { 
                    t = Activator.CreateInstance <T>(); 
                    propertypes = t.GetType().GetProperties(); 
                    foreach (PropertyInfo pro in propertypes) 
                    { 
                        tempName = pro.Name; 
                        if (table.Columns.Contains(tempName)) 
                        { 
                            object value = row[tempName]; 
                            pro.SetValue(t, value, null); 
                        } 
                    } 
                    list.Add(t); 
                } 
                return list; 
            } 
      

  2.   

    http://topic.csdn.net/u/20100614/17/ff8d7593-5b97-4bf3-8ac1-4637e912aa36.html1楼好像也有回答过我...
      

  3.   


    构造函数放在哪儿里 ,list类里  还是 这个 转换的类里谢谢 
      

  4.   

        public static IList<T> DataSetToIList<T>
            (DataSet p_DataSet, int p_TableIndex)
             where T : new()
        { 
            //
        }
      

  5.   

      private static readonly object objSync = new object();
            /// <summary>
            /// 将DataTable转换为list
            /// </summary>
            /// <typeparam name="T"></typeparam>
            /// <param name="dt"></param>
            /// <returns></returns>
            public static IList<T> DataTableToList<T>(DataTable dt)
            { 
                IList<T> result = new List<T>();
                try
                {
                    Monitor.Enter(objSync);
                    if (dt == null)
                        return null;                for (int j = 0; j < dt.Rows.Count; j++)
                    {
                        T _t = (T)Activator.CreateInstance(typeof(T));
                        PropertyInfo[] propertys = _t.GetType().GetProperties();
                        foreach (PropertyInfo pi in propertys)
                        {
                            for (int i = 0; i < dt.Columns.Count; i++)
                            {
                                // 属性与字段名称一致的进行赋值 
                                if (pi.Name.ToLower().Equals(dt.Columns[i].ColumnName.ToLower()))
                                {
                                    if (dt.Rows[j][i] != DBNull.Value)
                                    {
                                        if (pi.PropertyType.ToString() == "System.Int32" || pi.PropertyType.ToString() == "System.Nullable`1[System.Int32]")
                                        {
                                            pi.SetValue(_t, Int32.Parse(dt.Rows[j][i].ToString()), null);
                                        }
                                        if (pi.PropertyType.ToString() == "System.DateTime" || pi.PropertyType.ToString() == "System.Nullable`1[System.DateTime]")
                                        {
                                            pi.SetValue(_t, Convert.ToDateTime(dt.Rows[j][i].ToString()), null);
                                        }
                                        if (pi.PropertyType.ToString() == "System.String")
                                        {
                                            pi.SetValue(_t, dt.Rows[j][i].ToString(), null);
                                        }
                                        if (pi.PropertyType.ToString() == "System.Boolean")
                                        {
                                            pi.SetValue(_t, Convert.ToBoolean(dt.Rows[j][i].ToString()), null);
                                        }
                                        if (pi.PropertyType.ToString() == "System.Nullable`1[System.Decimal]")
                                        {
                                            pi.SetValue(_t, Convert.ToDecimal(dt.Rows[j][i].ToString()), null);
                                        }
                                    }
                                    else
                                        pi.SetValue(_t, null, null);
                                    break;
                                }
                            }
                        }
                        result.Add(_t);
                    }
                }
                finally
                {
                    Monitor.Exit(objSync);
                }
                return result;
            }