我正在尝试使用Boo的Reactive Extensions并遇到类型问题。这是基本的例子:
def OnSubscribe(observer as IObservable[of string]) as callable:
print "subscribing"
def Dispose():
print "disposing"
return Dispose
observable = System.Linq.Observable.Create[of string](OnSubscribe)
observer = System.Linq.Observer.Create[of string]({x as string | print x})
observable.Subscribe(observer)
订阅此处提供System.InvalidCastException:无法从源类型转换为目标类型。问题似乎在于我是如何创建可观察的,但我一直在努力查看类型问题的来源。
想法?
答案 0 :(得分:3)
Observable.Create
需要Func<IObserver,Action>
,但您的OnSubscribe
接受IObservable
。
试试这个:
def OnSubscribe(observer as IObserver[of string]) as callable():
print "subscribing"
observer.OnNext("first and only value")
observer.OnCompleted()
def Dispose():
print "disposing"
return Dispose
observable = System.Linq.Observable.Create[of string](OnSubscribe)
observer = System.Linq.Observer.Create[of string]({x as string | print x})
observable.Subscribe(observer)