There is a TypeDescriptor . It has a function GetProperties(object)

So, I take a collection of property descriptors and want to add a change handler to some.

 var props = TypeDescriptor.GetProperties(someClassObject); props[nameof(someClassObject.SomeProperty)].AddValueChanged(someClassObject, SomeHandler); 

This is where SomeHandler should be of type EventHandler . But the usual EventHandler has no parameters, and I need to pass into it, for example, the name of a property whose value has changed. I can not figure out how. In theory, the possibility should be. Just by logic. Otherwise, why is it even possible to thus subscribe to a change in a specific property, if you cannot find out the name of the changed property?

  • "The usual EventHandler does not have parameters," does it not? But what about object sender, EventArgs e ? - 4per
  • @ 3per and what can I pass to EventArgs? )) - iRumba
  • one
    an instance of any class inherited from EventArgs with your favorite properties you need - 4per
  • @ 3per, for some reason I didn’t manage to do it - iRumba
  • add code where it didn't work out - 4per

1 answer 1

In order for this to work, the class of the object being examined must implement the INotifyPropertyChanged interface, and the property being examined must trigger an event upon changes.

A small demo to understand:

 class Program : INotifyPropertyChanged { public int Prop1 { get; set; } private int _prop2 public int Prop2 { get { return _prop2; } set { _prop2 = value; OnPropertyChanged("Prop2"); } } public event PropertyChangedEventHandler PropertyChanged; private void OnPropertyChanged(string name) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name)); } private static void P_PropertyChanged(object sender, EventArgs e) { Console.WriteLine(e.GetType().ToString()); Console.WriteLine(sender.GetType().ToString()); if (e is PropertyChangedEventArgs) { var realArgs = (PropertyChangedEventArgs)e; Console.WriteLine(realArgs.PropertyName); } } static void Main(string[] args) { var p = new Program(); var props = TypeDescriptor.GetProperties(p); props[nameof(p.Prop1)].AddValueChanged(p, P_PropertyChanged); props[nameof(p.Prop2)].AddValueChanged(p, P_PropertyChanged); p.Prop1 = 1; p.Prop2 = 2; Console.ReadLine(); } } 

If you start and see the output of the program, it is easy to notice that for the Prop1 property the handler is not called, since This property does not report changes. For the property Prop2 when changing, the handler is called, since we have made sure in advance that the property notifies of changes and that the event handler is not passed empty EventArgs , but PropertyChangedEventArgs which contains the name of the property.

The only question that remains is: why such difficulties, if you can immediately subscribe to the PropertyChanged event, which is defined in the INotifyPropertyChanged interface without which all this is generally useless? Check that the class implements the interface and subscribe directly to the event without intermediaries seems to me much easier and more understandable.

  • Have you read the question carefully? How do I pass this SomeEventArgs ? - iRumba
  • Look at a piece of code ... at the end there is SomeHandler . This is the same handler. How do I pass on SomeEventArgs with him? - iRumba
  • It turned out a little longer than expected, but with a working demo - rdorn
  • Well in this case do not need such complexity. I somehow wanted to somehow magically subscribe to a change in the property, which itself does not notify about its change. - iRumba