I have a class that has a bunch of properties of certain type , this type has 3 events and I want to subscribe to this events on my class constructor, but avoiding typing each property name 3 times, is this possible?
Something like iterate trough all the properties of this type and add the event handlers?
internal class myValues {
public myValues () {
// Want to avoid hardcoding all my Properties here, and loop trough them all
Test1.AnyChanged += OnMetricChanged;
Test1.ValueChanged += OnMetricValueChanged;
Test1.DeltaChanged += OnMetricDeltaChanged;
...
...
// I can go trough all of them, but not sure how to add the handlers here
var properties = typeof(myValues).GetProperties().Where(p => p.PropertyType == typeof(Metric));
foreach (var p in properties) {
Debug.WriteLine(p.Name );
}
}
public Metric Test1 { get; private set; }
public Metric Test2 { get; private set; }
public Metric Test3 { get; private set; }
public Metric Test4 { get; private set; }
}