I have a main class where I am looping through each internal property in my cleats class property. Each internal property in my cleats class is of type BeltProperty
(another class that contains information like value and id's).
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...
}
On the first BeltProperty
it finds it throws a System.Reflection.TargetException
. I want to know if there is another/better way to get the property from my cleats class. Thanks in advance for any help or advice.
Well first i would choose better names for the classes and instances.
ObservableCollection<Cleats> Cleats
is not straight forward.
The issue you are having is because of this
parameter in .GetValue(this, null);
This parameter should be the instance that you are trying to read the property from.
The whole code could look like this:
foreach (PropertyInfo prop in typeof(Cleats)
.GetProperties(BindingFlags.Instance | BindingFlags.NonPublic))
{
var bp = (BeltProperty)prop.GetValue(Cleats[Configurator_2016.Cleats.SelectedConfigurationIndex])
}