Notify Changes on external parameter change

做~自己de王妃 提交于 2019-12-09 12:41:32

问题


I need to attach a notify change event to an encapsulated parameter in an external class. I usually would implement INotifyPropertyChanged, but I can't edit the external class. What is the right approach for this problem?


回答1:


That will be very hard to do. The best option seems to be deriving that class, but you need all properties to be marked virtual. When you have no control over the class that seems to be unlikely.

If you are the only one calling that class, you could also create a wrapper that mimics that class' behavior. You could create properties yourself and implement INotifyPropertyChanged. You can't get notified on changed to the inner object though.

If that is your best option, you could also implement a implicit conversion operator so you can pass in your class like it was the class you are wrapping.




回答2:


A brute force approach could be to use a timer which keeps looking at the value of that property every (say) 1 second and notifies you when the property value is not the same as the previous read. Timers use light-weight threads of their own, so this shouldn't be a lot of burden on the resources either. Looking at the tough situation that you're in, this seems to be the only viable option.



来源:https://stackoverflow.com/questions/25260044/notify-changes-on-external-parameter-change

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!