使用反射获取属性值时参数计数不匹配

编程入门 行业动态 更新时间:2024-10-24 12:20:23
本文介绍了使用反射获取属性值时参数计数不匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

我有以下代码:

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

在为我的数据表 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());

更多推荐

使用反射获取属性值时参数计数不匹配

本文发布于:2023-11-11 18:35:12,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1579245.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:反射   不匹配   属性   参数

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!