Skip to content

Instantly share code, notes, and snippets.

@itn3000
Last active January 24, 2024 08:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save itn3000/796f8422a0f24c408a4caa9f8888d76a to your computer and use it in GitHub Desktop.
Save itn3000/796f8422a0f24c408a4caa9f8888d76a to your computer and use it in GitHub Desktop.
R3 Observable creation on event which requires two arguments.
using R3;
var a = new A();
using var x1 = Observable.FromEvent<A.PiyoHandler, int>(h => new A.PiyoHandler(h), h => a.PiyoEvent += h, h => a.PiyoEvent -= h)
.Subscribe(x => Console.WriteLine($"piyo: {x}"));
using var x2 = Observable.FromEventHandler<int>(h => a.MogeEvent += h, h => a.MogeEvent -= h)
.Subscribe(x => Console.WriteLine($"moge: {x}"));
using var x3 = Observable.FromEvent<A.HogeHandler, (object?, int)>(h => new A.HogeHandler((sender, arg) => h((sender, arg))), h => a.HogeEvent += h, h => a.HogeEvent -= h)
.Subscribe(x => Console.WriteLine($"hoge: {x}"));
// See https://aka.ms/new-console-template for more information
a.X(new object(), 1);
class A
{
public delegate void HogeHandler(object? sender, int arg);
public delegate void PiyoHandler(int arg);
public event HogeHandler HogeEvent;
public event PiyoHandler PiyoEvent;
public event EventHandler<int> MogeEvent;
public void X(object? sender, int arg)
{
HogeEvent?.Invoke(sender, arg);
PiyoEvent?.Invoke(arg);
MogeEvent?.Invoke(sender, arg);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment