我得到了参数计数不匹配我不明白的错误。
我有以下代码:
Type target = Type.GetType("CPS_Service." + DocumentType);
// Create an instance of my target class
instance = Activator.CreateInstance(target);
foreach (XElement pQ in PQData.Elements())
{
try
{
// populate the member in the instance of the data class with the value from the MQ String
if (target.GetProperty(pQ.Attribute("name").Value) != null)
{
target.GetProperty(pQ.Attribute("name").Value).SetValue(instance, pqRequest[Convert.ToInt32(pQ.Attribute("pos").Value)], null);
}
}
}
PropertyInfo[] properties = target.GetProperties();
foreach (PropertyInfo property in properties)
{
DataColumn col = new DataColumn(property.Name);
col.DataType = System.Type.GetType("System.String");
col.DefaultValue = "";
dt.Columns.Add(col);
}
DataRow dr = dt.NewRow();
foreach (PropertyInfo property in properties)
{
string value = property.GetValue(instance).ToString();
dr[property.Name.ToString()] = "";
}
dt.Rows.Add(dr);
return dt; //
所以我实例化一个泛型类并从字符串数组(取自制表符分隔的字符串)填充它,然后我需要从该类输出列表或数据表instance
填充数据行时dr
对于我的数据表dt
我试图从课堂上获得价值:
string value = property.GetValue(instance, null).ToString();
dr[property.Name.ToString()] = "";
但上线了property.GetValue(instance).ToString();
我收到以下错误:
参数计数不匹配
我已经四处搜索,有关此错误的其他问题不适用......
或者我最好将我的班级放入列表中并将其返回?
如果您试图获取字符串(或任何具有索引器的类型)的所有属性的值,那么您将必须有一个特殊的情况来处理索引器。因此,如果您想获取该参数的值,则必须传递值的对象数组,其中一个参数作为您想要获取的索引值。
例如,property.GetValue(test, new object [] { 0 });
将获取索引 0 处字符串的值。因此,如果字符串的值是"ABC",结果将是'A'.
最简单的事情就是跳过索引器。您可以使用以下方法测试属性是否是索引器property.GetIndexParameters().Any()
。我认为您可以在调用时使用适当的绑定标志来跳过此检查GetProperties()
,但如果可以的话,我没有看到。
如果您想跳过代码中的索引,请更改:
PropertyInfo[] properties = target.GetProperties();
To:
var properties = target.GetProperties().Where(p => !p.GetIndexParameters().Any());
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)