Created
March 7, 2014 17:00
-
-
Save andybrackley/9415298 to your computer and use it in GitHub Desktop.
C# Convert non standard Event an Observable using Observable.FromEvent and the conversion function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Demo of how to convert an non-standard event into an Observable Sequence | |
// by using the Observable.FromEvent overload that uses a conversion Function. | |
public class SomeObservableClass | |
{ | |
public delegate void SettingChangedEventHandler(object sender, string setting); | |
public event SettingChangedEventHandler SettingChanged; | |
} | |
void Main() | |
{ | |
SomeObservableClass cls = new SomeObservableClass(); | |
Observable.FromEvent<SettingChangedEventHandler, string> | |
( handler_ => (sdr_, args_) => handler_(args_), | |
h => cls.SettingChanged += h, h => cls.SettingChanged -= h) // Define conversion here ... | |
.Subscribe(args => System.Diagnostics.Debug.WriteLine(args)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can omit the sender argument to simplified, easier to understand for beginner.