C# 被动扩展:包装自定义委托事件

C# 被动扩展:包装自定义委托事件,c#,system.reactive,C#,System.reactive,如何使用Observable.FromEvent在Rx中包装这些自定义委托 public delegate void EmptyDelegate(); public delegate void CustomDelegate( Stream stream, Dictionary<int, object> values ); public委托void EmptyDelegate(); 公共委托void CustomDelegate(流、字典值); EmptyDelegate 这是一个无

如何使用Observable.FromEvent在Rx中包装这些自定义委托

public delegate void EmptyDelegate();
public delegate void CustomDelegate( Stream stream, Dictionary<int, object> values );
public委托void EmptyDelegate();
公共委托void CustomDelegate(流、字典值);
EmptyDelegate 这是一个无参数委托,但流需要一个类型-Rx defines
Unit
,用于表示我们只对事件发生感兴趣的事件类型-即,没有有意义的负载

假设您有此委托的实例,请声明为:

public EmptyDelegate emptyDelegate;
然后你可以做:

var xs = Observable.FromEvent<EmptyDelegate, Unit>(
    h => () => h(Unit.Default),
    h => emptyDelegate += h,
    h => emptyDelegate -= h);

xs.Subscribe(_ => Console.WriteLine("Invoked"));

emptyDelegate(); // Invoke it
var xs = Observable.FromEvent<CustomDelegate, CustomEvent>(
    h => (s, v) => h(new CustomEvent { Stream = s, Values = v }),
    h => customDelegate += h,
    h => customDelegate -= h);

xs.Subscribe(_ => Console.WriteLine("Invoked"));

// some data to invoke the delegate with
Stream stream = null;
Dictionary<int,object> values = null;

// and invoke it
customDelegate(stream,values);
你可以做:

var xs = Observable.FromEvent<EmptyDelegate, Unit>(
    h => () => h(Unit.Default),
    h => emptyDelegate += h,
    h => emptyDelegate -= h);

xs.Subscribe(_ => Console.WriteLine("Invoked"));

emptyDelegate(); // Invoke it
var xs = Observable.FromEvent<CustomDelegate, CustomEvent>(
    h => (s, v) => h(new CustomEvent { Stream = s, Values = v }),
    h => customDelegate += h,
    h => customDelegate -= h);

xs.Subscribe(_ => Console.WriteLine("Invoked"));

// some data to invoke the delegate with
Stream stream = null;
Dictionary<int,object> values = null;

// and invoke it
customDelegate(stream,values);
var xs=Observable.FromEvent(
h=>(s,v)=>h(新CustomEvent{Stream=s,Values=v}),
h=>customDelegate+=h,
h=>customDelegate-=h);
订阅(=>Console.WriteLine(“已调用”);
//要调用委托的某些数据
Stream=null;
字典值=null;
//并调用它
customDelegate(流、值);
有关
可观察的详细说明,请参阅