需要 RX 方面的帮助:将 System.Diagnostics.Process.OutputDataReceived 转换为 Observable
我想启动一个子进程并观察它的重定向输出。那不是 C# 对我来说是一个问题,但我尝试理解 RX,所以游戏开始了...... 我有一个用于进程的静态扩展方法,如下所示:
public static IObservable<IEvent<DataReceivedEventArgs>> GetOutput(this Process that)
{
return Observable.FromEvent<DataReceivedEventArgs>(that, "OutputDataReceived");
}
我创建一个可观察对象并像这样订阅它:
Process p = ........
var outObs = p.GetOutput();
var outSub = outObs.Subscribe(data => Console.WriteLine(data));
这并不是完全错误,但我得到:
System.Collections.Generic.Event`1[System.Diagnostics.DataReceivedEventArgs]
虽然我期望获得字符串:-(
所以,我认为,我的扩展方法返回错误的类型。 如果有人能给我解释一下,那就太好了,什么是 Wong 与我的扩展方法签名。
多谢, ++马布拉
I want to start a subprocess and watch it's redirected output. That not
a problem for me in C#, but I try to understand RX, so the game begins ...
I have a static extension method for process, which looks like this:
public static IObservable<IEvent<DataReceivedEventArgs>> GetOutput(this Process that)
{
return Observable.FromEvent<DataReceivedEventArgs>(that, "OutputDataReceived");
}
I create an observable and subscribe to it like this:
Process p = ........
var outObs = p.GetOutput();
var outSub = outObs.Subscribe(data => Console.WriteLine(data));
This is not completely wrong, but I am getting:
System.Collections.Generic.Event`1[System.Diagnostics.DataReceivedEventArgs]
while I am expecting to get strings :-(
So, I think, my extensionmethod returns the wrong type.
It would be really good, if someone could explain me, what's
wong with my extension methods signature.
Thanks a lot,
++mabra
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
,就是这样。
IEvent 包装了传统事件委托的
sender
和EventArgs
参数。因此,您需要修改代码,使其看起来像这样:如果您使用的是最新的 Rx,那么代码有点不同,
这里的关键是
Select
中的EventArgs
EventPattern/IEvent,然后获取Data
That's exactly it.
IEvent wraps both the
sender
and theEventArgs
parameters of a tradition Event delegate. So you need to modify your code to look something likeIf you're using the latest Rx, then the code is a bit different
the key here is to
Select
theEventArgs
from the EventPattern/IEvent, and then grab theData