TargetParameterCountException when enumerating through properties of string

前端 未结 3 1449
梦如初夏
梦如初夏 2021-02-07 09:01

I\'m using the following code to output values of properties:

string output = String.Empty;
string stringy = \"stringy\";
int inty = 4;
Foo spong = new Foo() {Na         


        
3条回答
  •  误落风尘
    2021-02-07 09:35

    In this case, one of the string's properties is the indexer for returning the character at the specified location. Thus, when you try to GetValue, the method expects an index but doesn't receive one, causing the exception.

    To check which properties require index you can call GetIndexParameters on the PropertyInfo object. It returns an array of ParameterInfo, but you can just check the length of that array (if there are no parameters, it will be zero)

提交回复
热议问题