List集合转换为DataSet报:参数计数不匹配

逍遥游007 2010-04-17 02:52:31
我需要要将一个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;
}

转换过程中在红色部分代码处报:“参数计数不匹配
大家帮忙看看什么原因? 谢啦!
...全文
353 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
逍遥游007 2010-04-22
  • 打赏
  • 举报
回复
搞了有一段时间,好像行不通,换了种实现方式。用数组中的内容和某个类中的属性做匹配来实现动态赋值。
类似:
string [] str = new string[] {"abc","edf"};
for (int i = 0; i < str.Length; i++)
{
TemplateParm parameters = new TemplateParm();
Type temp = parameters.GetType();
PropertyInfo[] propertys = temp.GetProperties();
foreach (PropertyInfo info in propertys)
{
object value = info.GetValue(parm, null); //拿到TemplateParm类中非空的数据
//匹配数组中内容与需要替换的内容,匹配成功进行替换
if (str[i] == "[" + info.Name + "]")
{
content = content.Replace(str[i].ToString(), value.ToString());
}
}
}
Peter200694013 2010-04-17
  • 打赏
  • 举报
回复
PropertyInfo的GetValue(Object,Object[])

public virtual Object GetValue (
Object obj,
Object[] index
)
obj
将返回其属性值的对象。

index
索引化属性的可选索引值。对于非索引化属性 (Property),此值应为 空引用(在 Visual Basic 中为 Nothing)。

62,041

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧