.NET 이벤트를 관찰 가능한 시퀀스로 변환합니다.
네임스페이스:System.Reactive.Linq
어셈블리: System.Reactive(System.Reactive.dll)
Syntax
'Declaration
Public Shared Function FromEvent(Of TDelegate, TEventArgs) ( _
conversion As Func(Of Action(Of TEventArgs), TDelegate), _
addHandler As Action(Of TDelegate), _
removeHandler As Action(Of TDelegate) _
) As IObservable(Of TEventArgs)
'Usage
Dim conversion As Func(Of Action(Of TEventArgs), TDelegate)
Dim addHandler As Action(Of TDelegate)
Dim removeHandler As Action(Of TDelegate)
Dim returnValue As IObservable(Of TEventArgs)
returnValue = Observable.FromEvent(conversion, _
addHandler, removeHandler)
public static IObservable<TEventArgs> FromEvent<TDelegate, TEventArgs>(
Func<Action<TEventArgs>, TDelegate> conversion,
Action<TDelegate> addHandler,
Action<TDelegate> removeHandler
)
public:
generic<typename TDelegate, typename TEventArgs>
static IObservable<TEventArgs>^ FromEvent(
Func<Action<TEventArgs>^, TDelegate>^ conversion,
Action<TDelegate>^ addHandler,
Action<TDelegate>^ removeHandler
)
static member FromEvent :
conversion:Func<Action<'TEventArgs>, 'TDelegate> *
addHandler:Action<'TDelegate> *
removeHandler:Action<'TDelegate> -> IObservable<'TEventArgs>
JScript does not support generic types and methods.
형식 매개 변수
- TDelegate
대리자의 형식입니다.
- TEventArgs
이벤트의 유형입니다.
매개 변수
- 변환
형식: System.Func<Action<TEventArgs>, TDelegate>
지정된 이벤트 처리기를 기본 .NET 이벤트와 호환되는 대리자로 변환하는 데 사용되는 함수입니다. 결과 대리자는 addHandler 및 removeHandler 작업 매개 변수 호출에 사용됩니다.
- Addhandler
형식: System.Action<TDelegate>
지정된 이벤트 처리기를 기본 .NET 이벤트에 연결하는 작업입니다.
- removeHandler
형식: System.Action<TDelegate>
기본 .NET 이벤트에서 지정된 이벤트 처리기를 분리하는 작업입니다.
반환 값
형식: System.IObservable<TEventArgs>
기본 .NET 이벤트의 호출에 대한 데이터 표현을 포함하는 관찰 가능한 시퀀스입니다.
설명
FromEvent 연산자는 발생 시 기본 이벤트와 함께 제공되는 이벤트 인수의 관찰 가능한 시퀀스를 만듭니다. FromEvent 연산자는 Action<T> 형식의 대리자에서만 작동합니다. 따라서 변환 함수를 사용하여 기본 .NET 이벤트와 호환되는 이벤트 처리기를 만들어야 합니다. 이 항목의 예제 코드는 System.IO.FileSystemEventHandler 및 System.IO.RenamedEventHandler 대리자의 이 변환을 보여 줍니다.
예제
이 예제 코드에서는 FromEvent 연산자를 사용하여 System.IO.FileSystemWatcher에서 만들기, 이름 바꾸기 및 삭제 이벤트를 수신 대기하는 방법을 보여 줍니다. 이 예제에서는 C:\Users\Public 폴더에서 이러한 이벤트를 감시합니다. FromEvent 연산자는 Action<T> 형식의 대리자만 지원합니다. 따라서 이 예제에서는 변환 매개 변수를 사용하여 System T> 대리자를 System.IO.FileSystemEventHandler<및 System.IO.RenamedEventHandler 대리자로 변환하는 람다 식을 정의합니다. 이벤트는 관찰 가능한 각 시퀀스에 대한 구독을 사용하여 관찰됩니다. 각 이벤트는 콘솔 창에 기록됩니다.
using System;
using System.Reactive.Linq;
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 FromEvent operator to setup a subscription to the Created event. ***//
//*** ***//
//*** The first lambda expression performs the conversion of Action<FileSystemEventArgs> ***//
//*** to FileSystemEventHandler. The FileSystemEventHandler just calls the handler ***//
//*** passing the FileSystemEventArgs. ***//
//*** ***//
//*** The other lambda expressions add and remove the FileSystemEventHandler to and from ***//
//*** the event. ***//
//******************************************************************************************//
var fswCreated = Observable.FromEvent<FileSystemEventHandler, FileSystemEventArgs>(handler =>
{
FileSystemEventHandler fsHandler = (sender, e) =>
{
handler(e);
};
return fsHandler;
},
fsHandler => fsw.Created += fsHandler,
fsHandler => fsw.Created -= fsHandler);
fswCreated.Subscribe(e => Console.WriteLine("{0} was created.", e.FullPath));
//******************************************************************************************//
//*** Use the FromEvent operator to setup a subscription to the Renamed event. ***//
//*** ***//
//*** The first lambda expression performs the conversion of Action<RenamedEventArgs> ***//
//*** to RenamedEventHandler. The RenamedEventHandler just calls the handler passing the ***//
//*** RenamedEventArgs. ***//
//*** ***//
//*** The other lambda expressions add and remove the RenamedEventHandler to and from ***//
//*** the event. ***//
//******************************************************************************************//
var fswRenamed = Observable.FromEvent<RenamedEventHandler, RenamedEventArgs>(handler =>
{
RenamedEventHandler fsHandler = (sender, e) =>
{
handler(e);
};
return fsHandler;
},
fsHandler => fsw.Renamed += fsHandler,
fsHandler => fsw.Renamed -= fsHandler);
fswRenamed.Subscribe(e => Console.WriteLine("{0} was renamed to {1}.", e.OldFullPath, e.FullPath));
//******************************************************************************************//
//*** Use the FromEvent operator to setup a subscription to the Deleted event. ***//
//*** ***//
//*** The first lambda expression performs the conversion of Action<FileSystemEventArgs> ***//
//*** to FileSystemEventHandler. The FileSystemEventHandler just calls the handler ***//
//*** passing the FileSystemEventArgs. ***//
//*** ***//
//*** The other lambda expressions add and remove the FileSystemEventHandler to and from ***//
//*** the event. ***//
//******************************************************************************************//
var fswDeleted = Observable.FromEvent<FileSystemEventHandler, FileSystemEventArgs>(handler =>
{
FileSystemEventHandler fsHandler = (sender, e) =>
{
handler(e);
};
return fsHandler;
},
fsHandler => fsw.Deleted += fsHandler,
fsHandler => fsw.Deleted -= fsHandler);
fswDeleted.Subscribe(e => Console.WriteLine("{0} was deleted.", e.FullPath));
Console.WriteLine("Press ENTER to exit...\n");
Console.ReadLine();
}
}
}
다음 출력은 예제 코드를 사용하여 생성되었습니다.
Press ENTER to exit...
C:\Users\Public\New Text Document.txt was created.
C:\Users\Public\New Text Document.txt was renamed to C:\Users\Public\TestFile.txt.
C:\Users\Public\TestFile.txt was deleted.