List集合转换为DataSet报:参数计数不匹配
我需要要将一个string类型的集合转换成DataSet类型的,相关代码如下
string[] str= new string[] {"abc","edf"};
List<string> list = new List<string>();
for (int i = 0; i < str.Length; i++)
{
list.Add(str[i]);
}
DataSet ds = ParmToDataset.ToDataSet(list);
//集合转换为Dataset方法
public static DataSet ToDataSet(IList p_List)
{
DataSet result = new DataSet();
DataTable _DataTable = new DataTable();
if (p_List.Count > 0)
{
PropertyInfo[] propertys = p_List[0].GetType().GetProperties();
foreach (PropertyInfo pi in propertys)
{
_DataTable.Columns.Add(pi.Name, pi.PropertyType);
}
for (int i = 0; i < p_List.Count; i++)
{
ArrayList tempList = new ArrayList();
foreach (PropertyInfo pi in propertys)
{
object obj = pi.GetValue(p_List[i], null);
tempList.Add(obj);
}
object[] array = tempList.ToArray();
_DataTable.LoadDataRow(array, true);
}
}
result.Tables.Add(_DataTable);
return result;
}
转换过程中在红色部分代码处报:“参数计数不匹配”
大家帮忙看看什么原因? 谢啦!