什么是对的方法创建的IObservable的好办法? [英] What is a good way to create an IObservable for a method?

查看:145
本文介绍了什么是对的方法创建的IObservable的好办法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们说,我们有一个类:

Let's say, we have a class:

public class Foo
{
   public string Do(int param)
   {
   }
}

我倒要创建一个可观察正在由不要方式产生的值。做到这一点的方法之一是创建它正在从不要称为事件,并使用 Observable.FromEvent 以创建观察的。但不知何故,我不感觉良好的只是任务的缘故创建一个事件。 ?有没有更好的办法做到这一点。

I'd like to create an observable of values that are being produced by Do method. One way to do it would be to create an event which is being called from Do and use Observable.FromEvent to create the observable. But somehow I don't feel good about creation of an event just for the sake of the task. Is there a better way to do it?

推荐答案

马特的回答让我想到了这一点:

Matt's answer made me thinking about this:

public class Foo
{
    private readonly Subject<string> _doValues = new Subject<string>();

    public IObservable<string> DoValues { get { return _doValues; } }

    public string Do(int param)
    {
        var ret = (param * 2).ToString();
        _doValues.OnNext(ret);
        return ret;
    }
}


var foo = new Foo();
foo.DoValues.Subscribe(Console.WriteLine);
foo.Do(2);

这篇关于什么是对的方法创建的IObservable的好办法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆