Observable.ToEnumerable<TSource> 方法

将可观测序列转换为可枚举序列。

Namespace:System.Reactive.Linq
装配: System.Reactive.dll) 中的 System.Reactive (

语法

'Declaration
<ExtensionAttribute> _
Public Shared Function ToEnumerable(Of TSource) ( _
    source As IObservable(Of TSource) _
) As IEnumerable(Of TSource)
'Usage
Dim source As IObservable(Of TSource)
Dim returnValue As IEnumerable(Of TSource)

returnValue = source.ToEnumerable()
public static IEnumerable<TSource> ToEnumerable<TSource>(
    this IObservable<TSource> source
)
[ExtensionAttribute]
public:
generic<typename TSource>
static IEnumerable<TSource>^ ToEnumerable(
    IObservable<TSource>^ source
)
static member ToEnumerable : 
        source:IObservable<'TSource> -> IEnumerable<'TSource> 
JScript does not support generic types and methods.

类型参数

  • TSource
    源的类型。

参数

  • source
    类型: System.IObservable<TSource>
    要转换为可枚举序列的可观察序列。

返回值

类型: System.Collections.Generic.IEnumerable<TSource>
包含可观测序列中的元素的可枚举序列。

使用说明

在 Visual Basic 和 C# 中,可以将此方法作为 IObservable<TSource> 类型的任何对象的实例方法调用。 当使用实例方法语法调用此方法时,请省略第一个参数。 有关详细信息,请参阅

备注

ToEnumerator 运算符从可观测序列返回枚举器。 枚举器在生成时将生成序列中的每个项。

示例

以下示例创建一个可观测的整数序列。 间隔运算符每秒在序列中生成一个新整数。 可观测序列将转换为枚举器,并且每个项在生成时写入控制台窗口。

using System;
using System.Reactive.Linq;
using System.Threading.Tasks;

namespace Example
{
  class Program
  {
    static void Main()
    {
      //******************************************************//
      //*** Create an observable sequence of integers.     ***//
      //******************************************************//

      var obs = Observable.Interval(TimeSpan.FromSeconds(1));
  

      //*******************************************************//
      //*** Convert the integer sequence to an enumerable.  ***//
      //*******************************************************//

      var intEnumerable = obs.ToEnumerable();


      //*********************************************************************************************//
      //*** Create a task to enumerate the items in the list on a worker thread to allow the main ***//
      //*** thread to process the user's ENTER key press.                                         ***//
      //*********************************************************************************************//

      Task.Factory.StartNew(() =>
      {
        foreach (int val in intEnumerable)
        {
          Console.WriteLine(val);
        }
      });


      //*********************************************************************************************//
      //*** Main thread waiting on the user's ENTER key press.                                    ***//
      //*********************************************************************************************//

      Console.WriteLine("\nPress ENTER to exit...\n");
      Console.ReadLine();
    }
  }
}

以下输出是使用示例代码生成的。

 
Press ENTER to exit...

0
1
2
3
4
5
6
7
8
9

另请参阅

参考

可观测类

System.Reactive.Linq 命名空间