TargetParameterCountException when enumerating through properties of string

前端 未结 3 1448
梦如初夏
梦如初夏 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:37

    Just as reference... if you want to avoid the TargetParameterCountException when reading properties values:

    // Ask each childs to notify also, if any could (if possible)
    foreach (PropertyInfo prop in options.GetType().GetProperties())
    {
        if (prop.CanRead) // Does the property has a "Get" accessor
        {
            if (prop.GetIndexParameters().Length == 0) // Ensure that the property does not requires any parameter
            {
                var notify = prop.GetValue(options) as INotifyPropertyChanged; 
                if (notify != null)
                {
                    notify.PropertyChanged += options.OptionsBasePropertyChanged;
                }
            }
            else
            {
                // Will get TargetParameterCountException if query:
                // var notify = prop.GetValue(options) as INotifyPropertyChanged;
            }
        }
    }
    

    Hope it helps ;-)

提交回复
热议问题