Observable.FromEventPattern<TEventArgs> Method (Object, String)

Converts a .NET event, conforming to the standard .NET event pattern, to an observable sequence, using reflection to find an instance event.

Namespace:  System.Reactive.Linq
Assembly:  System.Reactive (in System.Reactive.dll)


Public Shared Function FromEventPattern(Of TEventArgs As EventArgs) ( _
    target As Object, _
    eventName As String _
) As IObservable(Of EventPattern(Of TEventArgs))
Dim target As Object
Dim eventName As String
Dim returnValue As IObservable(Of EventPattern(Of TEventArgs))

returnValue = Observable.FromEventPattern(target, _
public static IObservable<EventPattern<TEventArgs>> FromEventPattern<TEventArgs>(
    Object target,
    string eventName
where TEventArgs : EventArgs
generic<typename TEventArgs>
where TEventArgs : EventArgs
static IObservable<EventPattern<TEventArgs>^>^ FromEventPattern(
    Object^ target, 
    String^ eventName
static member FromEventPattern : 
        target:Object * 
        eventName:string -> IObservable<EventPattern<'TEventArgs>>  when 'TEventArgs : EventArgs
JScript does not support generic types and methods.

Type Parameters

  • TEventArgs
    The type of event.


  • target
    Type: System.Object
    The object instance that exposes the event to convert.
  • eventName
    Type: System.String
    The name of the event to convert.

Return Value

Type: System.IObservable<EventPattern<TEventArgs>>
The return value is an observable sequence that contains data representations of invocations of the underlying .NET event.


The FromEventPattern operator converts a .Net event to a sequence of EventPattern<TEventArgs>. Each EventPattern instance contains the event arguments and the object sending the event. The event arguments are provided in the EventArgs property of each EventPattern delivered in the sequence. The object sending the event is provided in the Sender property of the EventPattern instance. The desired event is specified by passing the object that exposes the event as the target parameter and by setting the eventName parameter to the name of the event. The TEventArgs type specifies the type of event arguments that will be delivered with each event.


This example code demonstrates using the FromEventPattern operator to listen for Create, Rename, and Delete events on a System.IO.FileSystemWatcher. The example watches for changes in the C:\Users\Public folder and writes the events to the console window.

using System;
using System.Reactive.Linq;
using System.Reactive;
using System.IO;

namespace Example
  class Program
    static void Main()
      //*** Create a FileSystemWatcher to watch the C:\Users\Public directory using the default NotifyFilter watching for ***//
      //*** changes to any type of file.                                                                                  ***//

      FileSystemWatcher fsw = new FileSystemWatcher(@"C:\Users\Public", "*.*");
      fsw.EnableRaisingEvents = true;

      //*** Use the FromEventPattern operator to setup a subscription to the Created event. ***//

      IObservable<EventPattern<FileSystemEventArgs>> fswCreated = Observable.FromEventPattern<FileSystemEventArgs>(fsw, "Created");
      fswCreated.Subscribe(pattern => Console.WriteLine("{0} was created in {1}.", pattern.EventArgs.Name, ((FileSystemWatcher)pattern.Sender).Path));

      //*** Use the FromEventPattern operator to setup a subscription to the Renamed event. ***//

      IObservable<EventPattern<RenamedEventArgs>> fswRenamed = Observable.FromEventPattern<RenamedEventArgs>(fsw, "Renamed");
      fswRenamed.Subscribe(pattern => Console.WriteLine("{0} was renamed to {1} in {2}.", pattern.EventArgs.OldName, 
                                                        pattern.EventArgs.Name, ((FileSystemWatcher)pattern.Sender).Path));

      //*** Use the FromEventPattern operator to setup a subscription to the Deleted event. ***//

      IObservable<EventPattern<FileSystemEventArgs>> fswDeleted = Observable.FromEventPattern<FileSystemEventArgs>(fsw, "Deleted");
      fswDeleted.Subscribe(pattern => Console.WriteLine("{0} was deleted in {1}.", pattern.EventArgs.Name, ((FileSystemWatcher)pattern.Sender).Path));

      Console.WriteLine("Press ENTER to exit...\n");

The following output demonstrates running the example code to create a new text file in the C:\Users\Public directory. The file is renamed to ExFile.txt and then deleted.

Press ENTER to exit...

New Text Document.txt was created in C:\Users\Public.
New Text Document.txt was renamed to ExFile.txt in C:\Users\Public.
ExFile.txt was deleted in C:\Users\Public.

See Also


Observable Class

FromEventPattern Overload

System.Reactive.Linq Namespace