使用反射获取 属性 的值时参数计数不匹配
Parameter count mismatch when getting value of property using Reflection
我收到一个我不明白的 参数计数不匹配 错误。
我有以下代码:
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; //
所以我正在实例化一个通用的 class 并从一个字符串数组(取自制表符分隔的字符串)填充它,然后我需要从 [=34= 输出一个列表或一个数据表] instance
为我的数据表 dt
填充数据行 dr
时,我试图从 class:
中获取值
string value = property.GetValue(instance, null).ToString();
dr[property.Name.ToString()] = "";
但是在 property.GetValue(instance).ToString();
行我得到以下错误:
Parameter count mismatch
我搜索了一下,关于这个错误的其他问题不适用...
或者将我的 class 转换为列表并返回它会更好吗?
如果您试图获取 String(或任何具有索引器的类型)的所有属性的值,那么您将必须有一个特殊情况来处理索引器。因此,如果您想获取该参数的值,则必须将一个参数的对象数组作为您想要获取的索引值传递。
例如,property.GetValue(test, new object [] { 0 });
将获取索引 0 处的字符串的值。因此,如果字符串的值为 "ABC",则结果将是 'A'.
最简单的方法就是跳过索引器。您可以使用 property.GetIndexParameters().Any()
测试 属性 是否是索引器。我以为您可以在调用 GetProperties()
时使用适当的绑定标志来跳过此检查,但如果可以的话,我没有看到它。
如果您想跳过代码中的索引,请更改:
PropertyInfo[] properties = target.GetProperties();
收件人:
var properties = target.GetProperties().Where(p => !p.GetIndexParameters().Any());
我收到一个我不明白的 参数计数不匹配 错误。
我有以下代码:
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; //
所以我正在实例化一个通用的 class 并从一个字符串数组(取自制表符分隔的字符串)填充它,然后我需要从 [=34= 输出一个列表或一个数据表] instance
为我的数据表 dt
填充数据行 dr
时,我试图从 class:
string value = property.GetValue(instance, null).ToString();
dr[property.Name.ToString()] = "";
但是在 property.GetValue(instance).ToString();
行我得到以下错误:
Parameter count mismatch
我搜索了一下,关于这个错误的其他问题不适用...
或者将我的 class 转换为列表并返回它会更好吗?
如果您试图获取 String(或任何具有索引器的类型)的所有属性的值,那么您将必须有一个特殊情况来处理索引器。因此,如果您想获取该参数的值,则必须将一个参数的对象数组作为您想要获取的索引值传递。
例如,property.GetValue(test, new object [] { 0 });
将获取索引 0 处的字符串的值。因此,如果字符串的值为 "ABC",则结果将是 'A'.
最简单的方法就是跳过索引器。您可以使用 property.GetIndexParameters().Any()
测试 属性 是否是索引器。我以为您可以在调用 GetProperties()
时使用适当的绑定标志来跳过此检查,但如果可以的话,我没有看到它。
如果您想跳过代码中的索引,请更改:
PropertyInfo[] properties = target.GetProperties();
收件人:
var properties = target.GetProperties().Where(p => !p.GetIndexParameters().Any());