What is a good way to create an IObservable for a method?

后端 未结 5 1914
攒了一身酷
攒了一身酷 2021-02-05 11:30

Let\'s say, we have a class:

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

I\'d like to create an observable of values that are

5条回答
  •  有刺的猬
    2021-02-05 12:06

    Matt's answer made me thinking about this:

    public class Foo
    {
        private readonly Subject _doValues = new Subject();
    
        public IObservable 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);
    

提交回复
热议问题