Parameter Count Mismatch exception when calling PropertyInfo.GetValue

后端 未结 3 490
谎友^
谎友^ 2020-12-11 15:06

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

         


        
相关标签:
3条回答
  • 2020-12-11 15:10

    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);
        ....
      }
    }
    
    0 讨论(0)
  • 2020-12-11 15:13

    This was plenty for me to skip over indexers.

    obj.GetType().GetProperties().Where(x => !x.GetIndexParameters().Any())
    
    0 讨论(0)
  • 2020-12-11 15:23

    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.

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

    0 讨论(0)
提交回复
热议问题