从非标准事件创建可观察对象(无 EventArgs / EventHandler)



我想为定义如下的事件创建一个可观察量:

public event Func<Exception, Task> Closed;

我目前的代码是这样的:

Observable.FromEvent<Func<Exception, Task>, Unit>(h => hub.Closed += h, h=> hub.Closed -= h); 

它编译正常,但它会抛出以下运行时异常:

System.ArgumentException:"无法绑定到目标方法,因为 其签名或安全透明度与 代表类型。

我觉得我做错了。我不习惯从不遵循 EventArgs 模式😔的事件创建可观察量

编辑:只是为了澄清,这是经典事件处理的完整代码:

class Program
{
static async Task Main(string[] args)
{
var hub = new HubConnectionBuilder().WithUrl("http://localhost:49791/hubs/status")
.Build();
hub.On<Status>("SendAction", status => Console.WriteLine($"Altitude: {status.Altitude:F} m"));
await hub.StartAsync();
hub.Closed += HubOnClosed;
while (true)
{
}
}
private static Task HubOnClosed(Exception arg)
{
Console.WriteLine("The connection to the hub has been closed");
return Task.CompletedTask;
}
}

您需要转换重载。每次我查看这个东西时,我都会关闭:

IObservable<TEventArgs> Observable.FromEvent<TDelegate, TEventArgs>(
Func<Action<TEventArgs>, TDelegate> conversion, 
Action<TDelegate> addHandler, 
Action<TDelegate> removeHandler>
)

所以在我们的例子中,TEventArgsException,而TDelegateFunc<Exception, Task>,所以你需要将Action<Exception>转换为Func<Exception, Task>>,换句话说:Func<Action<Exception>, Func<Exception, Task>>。我假设转换看起来像这样:a => e => {a(e); return Task.CompletedTask; }.

System.Reactive 需要此转换函数,因为它需要使用适当的委托订阅事件,并以某种方式挂钩您的代码/RX 管道代码。在这种情况下,a(e)基本上是 RX 管道,然后传递异常,以便稍后在反应式管道中处理。

完整代码:

class Program
{
static async Task Main(string[] args)
{
Program.Closed += Program.HubOnClosed;
Observable.FromEvent<Func<Exception, Task>, Exception>(
a => e => {a(e); return Task.CompletedTask; }, 
h => Program.Closed += h, 
h => Program.Closed -= h
)
.Subscribe(e =>
{
Console.WriteLine("Rx: The connection to the hub has been closed");
});
Program.Closed.Invoke(null);
Program.Closed.Invoke(null);
}
private static Task HubOnClosed(Exception arg)
{
Console.WriteLine("The connection to the hub has been closed");
return Task.CompletedTask;
}
public static event Func<Exception, Task> Closed;
}

这样的事情能解决问题吗?

class Program
{
public event Func<Exception, Task> Closed;
static void Main(string[] args)
{
Program p = new Program();
IObservable<Unit> closedObservable = Observable.Create<Unit>(
observer =>
{
Func<Exception, Task> handler = ex =>
{
observer.OnNext(Unit.Default);
return Task.CompletedTask;
};
p.Closed += handler;
return () => p.Closed -= handler;
});
}
}

对于此类异常情况,Observable.Create()是一个有用的回退。

顺便说一句,有一个带有非 void 返回委托的事件是非常奇怪的,因为引发事件的代码只会看到最后一个要运行的处理程序的值 - 除非它以某种非标准方式引发事件。但是,由于它是库代码,因此无法控制!

尝试以下方法,不使用您想要的签名,而是尝试一些东西:

class Program
{
public delegate void ClosedEventHandler(object sender, Func<Exception, Task> e);
public ClosedEventHandler Closed { get; set; }    
static void Main(string[] args)
{
Program hub = new Program();
hub.Closed = hub.SomethingToDoWhenClosed;    
Observable
.FromEventPattern<ClosedEventHandler, Func<Exception, Task>>(
h => hub.Closed += h,
h => hub.Closed -= h)
.Subscribe(x =>
{
// this is hit
});    
hub.Closed(hub, e => null);
}
public void SomethingToDoWhenClosed(object sender, Func<Exception, Task> e)
{
}
}

相关内容

  • 没有找到相关文章

最新更新