反应性扩展中缺少方法

时间:2012-04-24 14:30:57

标签: system.reactive eventaggregator

我正在尝试使用RX实现事件聚合器(反应式扩展)这里是我正在使用的代码,但是 subject.AddDisposable 方法丢失。谁能帮我?也许它是旧版本所以在新版本的Rx中这个方法被删除了?如果是这种情况,那么实施这个的方法是什么?

 if (_observablesByTypeKey.ContainsKey(key))
            {
                Tuple<object, object> tuple = _observablesByTypeKey[key];
                stream = (IObservable<T>)tuple.Item2;
            }
            else
            {
                Type specificSubjectType = typeof(Subject<>).MakeGenericType(new[] { typeof(T) });
                var subject = (Subject<T>)Activator.CreateInstance(specificSubjectType, new object[] { });

                var removeEventStreamFromCache = Disposable.Create(
                    () =>
                        {
                            lock (_observablesByTypeKeyLock)
                            {
                                _observablesByTypeKey.Remove(key);
                            }
                        }
                    );

                stream = subject.AddDisposable(removeEventStreamFromCache).Publish().RefCount();

                var tuple = new Tuple<object, object>(subject, stream);
                _observablesByTypeKey.Add(key, tuple);

1 个答案:

答案 0 :(得分:2)

您可以替换

stream = subject.AddDisposable(removeEventStreamFromCache).Publish().RefCount();

stream = Observable.Create(observer => 
  new CompositeDisposable(
    subject.Subscribe(observer),
    removeEventStreamFromCache));
stream.Publish().RefCount();