Castom EventHandler



  • Got it. TypeDescriptor♪ He has a function. GetProperties(object)

    So, I'm taking a collection of character descriptors, and I'd like to add some processors to change.

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

    Here. SomeHandler must have a type EventHandler♪ But the usual Ivant-Handler doesn't have parameters, and I need to give him, for example, the name of the properties that have changed. I can't figure it out. The idea should be an opportunity. It's logical. Otherwise, why would it even be possible to sign in such a way as to change a particular characteristic if the name of the modified characteristic was not known?



  • In order for this to work, the class of the object under investigation has to operate the interface. INotifyPropertyChangedand the properties examined should initiate an event at a time of change.

    A small demonstration 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 programme's conclusion, it's easy to see that for properties. Prop1 The processor is not called, as this characteristic does not notify the changes. For properties Prop2 When the processor's changed, we took care in advance that the properties would be notified of the changes and that the processor would not be transmitted empty. EventArgsPropertyChangedEventArgs which contains the name of the properties.

    The only question remains: why such difficulties should we sign immediately for the event PropertyChangedas defined in the interface INotifyPropertyChanged without which all this is, in general, useless? To make sure the class runs the interface and signed directly for an event without intermediaries, I think it's much easier and clearer.




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2