Firing an event / function on a property? (C#)

后端 未结 8 1476
你的背包
你的背包 2021-02-08 11:46

I am using a class that I cannot edit, it has a property (a boolean) of which it would be nice to be informed when it changes, I can\'t edit the properties get or set as I am im

8条回答
  •  被撕碎了的回忆
    2021-02-08 12:42

    Arguably, the only real way to do this is to create some kind of "watcher" component, running in a separate thread, whose job is to read the property at intervals and raise an event when the property's value changes. Of course this solution sails in the murky waters of threading and synchronization.

    On the assumption that your application is single-threaded in respect to this object, your cleanest solution is to make method calls to this object via a proxy object. It would have the job of checking the before and after state of the property and raising an event in the case it has changed.

    Here's a simple example of what I'm talking about:

    public class SomeProxy
    {
        public SomeProxy(ExternalObject obj)
        {
             _obj = obj;
        }
    
        public event EventArgs PropertyChanged;
    
        private bool _lastValue;
    
        private ExternalObject _obj;
    
        protected virtual void OnPropertyChanged()
        {
            if(PropertyChanged != null)
                PropertyChanged();
        }
    
        protected virtual void PreMethodCall()
        {
            _lastValue = _obj.SomeProperty;
        }
    
        protected virtual void PostMethodCall()
        {
            if(_lastValue != _obj.SomeProperty)
                OnPropertyChanged();
        }
    
        // Proxy method.
        public SomeMethod(int arg)
        {
            PreMethodCall();
            _obj.SomeMethod(arg); // Call actual method.
            PostMethodCall();
        }
    }
    

    Obviously you can build this proxy pattern into a suitable object - you just have to be aware that all calls have to go through the proxy for the event to be raised when you expect it to be.

提交回复
热议问题