使用反射获取属性值时参数计数不匹配 [英] Parameter count mismatch when getting value of property using Reflection

查看:120
本文介绍了使用反射获取属性值时参数计数不匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到一个我不明白的参数计数不匹配错误.

我有以下代码:

Type target = Type.GetType("CPS_Service." + DocumentType);//创建目标类的实例实例 = Activator.CreateInstance(目标);foreach(PQData.Elements() 中的 XElement pQ){尝试{//使用 MQ 字符串中的值填充数据类实例中的成员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[] 属性 = target.GetProperties();foreach(属性中的PropertyInfo 属性){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.GetValue(instance).ToString();dr[property.Name.ToString()] = "";}dt.Rows.Add(dr);返回 dt;//

所以我正在实例化一个通用类并从一个字符串数组(取自一个制表符分隔的字符串)中填充它,然后我需要从 instance<类中输出一个 List 或一个数据表/p>

在为我的数据表 dt 填充数据行 dr 时,我试图从类中获取值:

string value = property.GetValue(instance, null).ToString();dr[property.Name.ToString()] = "";

但在行 property.GetValue(instance).ToString(); 我收到以下错误:

<块引用>

参数计数不匹配

我已经四处搜索,但有关此错误的其他问题不适用...

或者我将我的类转换为 List 并返回它会更好吗?

解决方案

如果您试图获取 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());

I am getting a Parameter Count mismatch error which I don't understand.

I have the below code:

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; //

so I am instantiating a generic class and populating it from a string array (taken from a tab-delimited string), then I need to either output a List or a datatable from the class instance

When populating the datarow dr for my datatable dt I am trying to get the value from the class:

string value = property.GetValue(instance, null).ToString();
dr[property.Name.ToString()] = "";

but on the line property.GetValue(instance).ToString(); I get the below error:

Parameter count mismatch

I have searched around and the other questions about this error do not apply...

Or would I be better off just casting my class to a List and returning that?

解决方案

If you're trying to get the value of all properties of a String (or any type that has an indexer), then you're going to have to have a special case to deal with the indexer. So if you wanted to get the value of that parameter you'd have to pass the object array of values with one parameter as the index value you wanted to get.

For example, property.GetValue(test, new object [] { 0 }); would get the value of the string at index 0. So if the string's value was "ABC", the result of would be 'A'.

The easiest thing would be just to skip the indexer. You can test if a property is an indexer by using property.GetIndexParameters().Any(). I thought you could skip this check by using the appropriate binding flag when calling GetProperties(), but if you can, I didn't see it.

If you want to skip the index in your code, then change:

PropertyInfo[] properties = target.GetProperties(); 

To:

var properties = target.GetProperties().Where(p => !p.GetIndexParameters().Any());

这篇关于使用反射获取属性值时参数计数不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆