通知对外部参数变化而变化参数、通知

2023-09-03 02:07:27 作者:  妖小懒╰

我需要附加一个通知更改事件封装的参数,在外部类。我通常会实施 INotifyPropertyChanged的,但我不能编辑外部类。什么是正确的做法对于这个问题?

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?

推荐答案

这将是非常困难的事情。最好的办法似乎是派生的类,但你需要的所有属性进行标记虚拟。当你拥有了,这似乎是不太可能的类没有控制权。

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.

如果你是唯一一个调用这个类,你也可以创建一个包装,模仿该类的行为。你可以自己创建的属性和实施 INotifyPropertyChanged的。你不能就变更通知虽然内部对象。

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.