调用PropertyInfo.GetValue时参数计数不匹配异常

时间:2022-12-03 10:45:08

I'm trying to compare two objects at runtime using reflection to loop through their properties using the following method:

我正在尝试使用反射在运行时比较两个对象,以使用以下方法遍历其属性:

Private Sub CompareObjects(obj1 As Object, obj2 As Object)

    Dim objType1 As Type = obj1.GetType()

    Dim propertyInfo = objType1.GetProperties

    For Each prop As PropertyInfo In propertyInfo
        If prop.GetValue(obj1).Equals(prop.GetValue(obj2)) Then
            'Log difference here
        End If
    Next
End Sub

Whenever I test this method, I'm getting a Parameter Count Mismatch exception from System.Reflection.RuntimeMethodInfo.InvokeArgumentsCheck when it calls prop.GetValue(obj1).

每当我测试这个方法时,我会在调用prop.GetValue(obj1)时从System.Reflection.RuntimeMethodInfo.InvokeArgumentsCheck中获取参数计数不匹配异常。

This happens no matter the type of obj1 and obj2, nor the type in prop (in my test case, the first property is a Boolean).

无论obj1和obj2的类型,还是prop中的类型(在我的测试用例中,第一个属性是布尔值)都会发生这种情况。

What am I doing wrong and how can I fix it so that I can compare the values from the two objects?

我做错了什么以及如何修复它以便我可以比较两个对象的值?

Solution

I added the following lines just inside the for each loop:

我在for循环中添加了以下几行:

Dim paramInfo = prop.GetIndexParameters
If paramInfo.Count > 0 Then Continue For

The first property was taking a parameter, which was causing the problem. For now, I'll just skip any property that requires a parameter.

第一个属性是一个参数,这导致了问题。现在,我将跳过任何需要参数的属性。

3 个解决方案

#1


25  

I suspect your type contains an indexer - i.e. a property which takes parameters. You can check for this by calling PropertyInfo.GetIndexParameters and checking if the returned array is empty.

我怀疑你的类型包含一个索引器 - 即一个带参数的属性。您可以通过调用PropertyInfo.GetIndexParameters并检查返回的数组是否为空来检查这一点。

(If that isn't the problem, please edit your question to show a short but complete program demonstrating the issue.)

(如果这不是问题,请编辑您的问题以显示一个简短但完整的程序来证明问题。)

#2


1  

for C#:

PropertyInfo property = .....
ParameterInfo[] ps  = property.GetIndexParameters();
if (ps.Count() > 0)
{
  if(obj.ToString().Contains("+"))
  {
      Debug.Write("object is multi-type");
  }
  else { 
    var propValue = property.GetValue(obj, null);
    ....
  }
}

#3


0  

This was plenty for me to skip over indexers.

这对我来说很容易跳过索引器。

obj.GetType().GetProperties().Where(x => !x.GetIndexParameters().Any())

#1


25  

I suspect your type contains an indexer - i.e. a property which takes parameters. You can check for this by calling PropertyInfo.GetIndexParameters and checking if the returned array is empty.

我怀疑你的类型包含一个索引器 - 即一个带参数的属性。您可以通过调用PropertyInfo.GetIndexParameters并检查返回的数组是否为空来检查这一点。

(If that isn't the problem, please edit your question to show a short but complete program demonstrating the issue.)

(如果这不是问题,请编辑您的问题以显示一个简短但完整的程序来证明问题。)

#2


1  

for C#:

PropertyInfo property = .....
ParameterInfo[] ps  = property.GetIndexParameters();
if (ps.Count() > 0)
{
  if(obj.ToString().Contains("+"))
  {
      Debug.Write("object is multi-type");
  }
  else { 
    var propValue = property.GetValue(obj, null);
    ....
  }
}

#3


0  

This was plenty for me to skip over indexers.

这对我来说很容易跳过索引器。

obj.GetType().GetProperties().Where(x => !x.GetIndexParameters().Any())