Observable.FromEvent,无法获得正确的签名

时间:2012-12-19 14:31:33

标签: system.reactive

我试图从以下事件创建一个IObservable:

public delegate bool Handle<in T>(T obj, Foo foo);

 event Handle<Bar> Finance;

有关如何正确创建可观察对象的任何建议吗?

注意,Foo和Bar都不基于EventArgs

谢谢!

1 个答案:

答案 0 :(得分:2)

您可以尝试以下内容:

public class Test
{
    public delegate void Handle<in T>(T obj, Foo foo);

    public event Handle<Bar> Finance;
}

public static class TestEx
{
    static IObservable<Tuple<Bar, Foo>> WhenFinanceChanged(this Test t)
    {
        return Observable.Create<Tuple<Bar, Foo>>(observer =>
        {
            Test.Handle<Bar> handler = (obj, foo) => observer.OnNext(Tuple.Create<Bar, Foo>(obj, foo));
            t.Finance += handler;
            return Disposable.Create(() => t.Finance -= handler);
        });
    }
}