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

后端 未结 5 1916
攒了一身酷
攒了一身酷 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 11:57

    Observable.Generate solves your problem, although you don't need to use the condition and the continue for your sample. This just creates and object and continually returns the result of the call to DoIt():

    Observable.Generate (
      new Foo(),
      item => true, // in your example, this never terminates
      item => item, // we don't actually do any state transitions
      item => { return item.DoIt(); }  // This is where we emit our value
      );
    

    In practice, you often do want to track some state and have some termination condition. Generate makes it easy.

提交回复
热议问题