反应式扩展 IObservable 在订阅者订阅时发出通知
我有一个 Subject
,我希望在订阅它时收到通知。我找不到这方面的机制。我错过了什么吗?
例如:
public class AccountManager
{
private ReplaySubject<string> _accountEvents = new ReplaySubject<string>();
public AccountManager()
{
}
public void Add(string val)
{
_accountEvents.OnNext(val);
}
public IObservable<string> AccountEvents { get { return _accountEvents.AsObservable<string>(); } }
}
当任何其他代码调用 AccountEvents
属性上的 Subscribe
时,是否可以通知 AccountManager
。
I have a Subject<T>
and I want to be notified when something subscribes to it. I cannot find a mechanism for this. Am I missing something?
For example:
public class AccountManager
{
private ReplaySubject<string> _accountEvents = new ReplaySubject<string>();
public AccountManager()
{
}
public void Add(string val)
{
_accountEvents.OnNext(val);
}
public IObservable<string> AccountEvents { get { return _accountEvents.AsObservable<string>(); } }
}
Can AccountManager
be notified when any other code calls Subscribe
on the AccountEvents
property.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
无论是这个
还是(类似于斯科特的答案)
在第一种情况下,你有更多的自由,因为你有一个观察者作为参数传入,所以除了将主题输出路由到观察者之外,你还可以在以下情况下调用observer.OnNext,OnError,OnComplete:你的逻辑要求它。
Either this
or (similar to Scott's answer)
In the first case you have more freedom, since you have an observer passed in as a parameter, so in addition to routing subject output to the observer you can call observer.OnNext, OnError, OnComplete when your logic demands it.
这一切都很丑陋,但可能会起作用
This is ugly all, but could possibly be made to work