# /// <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.
无参数构造函数定义为这个对象。查了好久 谢谢
# /// 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.
无参数构造函数定义为这个对象。查了好久 谢谢
解决方案 »
- 我用了jquery-easyui,怎么就不能有<form >标签了
- 数据库搭建选择什么工具?
- 关于checkbox取值的老问题,我弄了两天加一个通宵就是搞不定,大家帮帮忙,来看看
- 在游戏战斗中,要求玩家多少秒内不能发招.这种需求要如何实现呢?只要说明思路.
- Application導致的怪怪的問題!!!!!!!!!!!!!!!!!!
- TreeView节点连入access数据库中列的问题(今天结贴)
- 请问怎样把水晶报表拉宽?
- .net 2.0不装IIS也可以用吗?
- DataGrid的HyperLinkColumn 问题
- 项目调试出错设为“禁止调试”以后如何恢复为可调试?
- 倾家荡产求救 .net2008程序上传服务器问题
- 状态栏进度条问题
{
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;
}
构造函数放在哪儿里 ,list类里 还是 这个 转换的类里谢谢
(DataSet p_DataSet, int p_TableIndex)
where T : new()
{
//
}
/// <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;
}