0

我有一个主类,我在其中循环遍历我的夹板类属性中的每个内部属性。我的夹板类中的每个内部属性都是类型BeltProperty(另一个包含值和 id 等信息的类)。

private ObservableCollection<Cleats> _Cleats = new ObservableCollection<Cleats>();
    /// <summary>
    /// Cleats
    /// </summary>
    public ObservableCollection<Cleats> Cleats { get { return _Cleats; } }

foreach (PropertyInfo prop in typeof(Cleats)
    .GetProperties(BindingFlags.Instance | BindingFlags.NonPublic))
{
    BeltProperty bp = new BeltProperty();
    bp = (BeltProperty)Cleats[Configurator_2016.Cleats.SelectedConfigurationIndex]
        .GetType().GetProperty(prop.Name, BindingFlags.Instance | BindingFlags.NonPublic)
        .GetValue(this, null);
    //rest of the code...
}

在第一次BeltProperty它发现它抛出一个System.Reflection.TargetException. 我想知道是否有另一种/更好的方法可以从我的夹板类中获取属性。提前感谢您的任何帮助或建议。

4

1 回答 1

0

首先我会为类和实例选择更好的名称。
ObservableCollection<Cleats> Cleats不是直截了当。

您遇到的问题是由于this参数.GetValue(this, null);

此参数应该是您尝试从中读取属性的实例。

整个代码可能如下所示:

foreach (PropertyInfo prop in typeof(Cleats)
    .GetProperties(BindingFlags.Instance | BindingFlags.NonPublic))
{
    var bp = (BeltProperty)prop.GetValue(Cleats[Configurator_2016.Cleats.SelectedConfigurationIndex])
}
于 2017-06-02T15:21:24.480 回答