A class implementing two different IObservables?

旧城冷巷雨未停 提交于 2019-12-11 03:47:33

问题


I have a class with two events, call them StatusChanged and ValueChanged. I'm wondering about exposing these 'streams' as IObservable. Is implementing IObservable<Status> and IObservable<Value> on the same class 'bad'? Is it likely to cause me (or users of my class) grief?


回答1:


Implementing a covariant interface for different types is a really bad idea. Consider what happens if you cast the class to IObservable<object>, which is now ambiguous.

I'd rather have two properties IObservable<Status> StatusObservable{get{...}} and IObservable<Value> ValueObservable{get{...}}. Simple, clean and it mirrors the two events your class offers.



来源:https://stackoverflow.com/questions/14560134/a-class-implementing-two-different-iobservables

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