重大变更破坏了 Rx 样本

发布于 2024-12-25 10:41:37 字数 729 浏览 0 评论 0 原文

Rx Wiki 的 101 个 Rx 示例中的一些示例实际上不再起作用。我认为这是因为我拥有最新的 Rx 版本(1.0.10621.2),但我想确认并看看如何让它们工作。

例如,以下示例代码不起作用:

class ObserveEvent_Generic
{
    public class SomeEventArgs : EventArgs { }
    public static event EventHandler<SomeEventArgs> GenericEvent;

    static void Main()
    {
        // To consume GenericEvent as an IObservable:
        IObservable<IEvent<SomeEventArgs>> eventAsObservable = Observable.FromEvent<SomeEventArgs>(
            ev => GenericEvent += ev,
            ev => GenericEvent -= ev);
    }
}

缺少 IEvent 并且“ev => GenericEvent += ev”导致“无法将类型 System.Action 隐式转换为 <代码>System.EventHandler 错误。

There are a few samples from the 101 Rx Samples from Rx Wiki that don't actually work anymore. I think it's because the latest Rx version I have (1.0.10621.2) but I'd like to confirm and see how I can get them to work.

For example, the following sample code doesn't work:

class ObserveEvent_Generic
{
    public class SomeEventArgs : EventArgs { }
    public static event EventHandler<SomeEventArgs> GenericEvent;

    static void Main()
    {
        // To consume GenericEvent as an IObservable:
        IObservable<IEvent<SomeEventArgs>> eventAsObservable = Observable.FromEvent<SomeEventArgs>(
            ev => GenericEvent += ev,
            ev => GenericEvent -= ev);
    }
}

The IEvent is missing and "ev => GenericEvent += ev" is causing a "Cannot implicitly convert type System.Action<xxx> to System.EventHandler<xxx> error.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

苏佲洛 2025-01-01 10:41:37

在这种情况下,您有几种选择。第一个是提供与标准 EventHandler 模式匹配的委托(生成的 Observable 的类型为 IObservable)。请注意,我提供的 lamda 接受“o”(“发送者”),但未使用此参数:

IObservable<SomeEventArgs> eventAsObservable = 
Observable.FromEvent<SomeEventArgs>
(
    ev => GenericEvent += (o,e) => ev(e),
    ev => GenericEvent -= (o,e) => ev(e)
);

特别是在您的情况下(静态事件...哎呀!),我将不愿信任取消订阅会真正正确地清理,并且根据使用情况,您可能最终会出现严重的内存泄漏。作为替代方案,您可以指定委托类型。注意这里 FromEvent 的额外泛型参数

IObservable<SomeEventArgs> eventAsObservable = 
Observable.FromEvent<EventHandler<SomeEventArgs>, SomeEventArgs>
(
    ev => GenericEvent += ev,
    ev => GenericEvent -= ev
);

另一种替代方案是将源/发送者对象的处理移至观察者(使可观察类型为 IObservable>) 是使用 FromEventPattern 方法。我在某些构建中使用此方法遇到了一些麻烦,因此您的情况可能会有所不同,但这是替代方案:

IObservable<EventPattern<SomeEventArgs>> eventAsObservable = 
Observable.FromEventPattern<SomeEventArgs>
(
    ev => GenericEvent += ev,
    ev => GenericEvent -= ev
);

希望这会有所帮助。

You have a few options in this case. The first is to provide a delegate that matches the standard EventHandler pattern (the resulting Observable is of type IObservable<SomeEventArgs>). Notice the lamda I provided accepts "o" (the "sender"), but this param is not used:

IObservable<SomeEventArgs> eventAsObservable = 
Observable.FromEvent<SomeEventArgs>
(
    ev => GenericEvent += (o,e) => ev(e),
    ev => GenericEvent -= (o,e) => ev(e)
);

Especially in your situation (static event... yikes!), I would be reticent to trust that the unsubscribe would truly cleanup properly and you might end up with a nasty memory leak depending on usage. As an alternative, you can specify the delegate type. Notice here the extra generic parameter to FromEvent

IObservable<SomeEventArgs> eventAsObservable = 
Observable.FromEvent<EventHandler<SomeEventArgs>, SomeEventArgs>
(
    ev => GenericEvent += ev,
    ev => GenericEvent -= ev
);

One more alternative that moves the handling of the source/sender object to the Observer (making the observable of type IObservable<EventPattern<SomeEventArgs>>) is to use the FromEventPattern method. I've had a little trouble with this method in some builds, so your mileage may vary, but here is that alternative:

IObservable<EventPattern<SomeEventArgs>> eventAsObservable = 
Observable.FromEventPattern<SomeEventArgs>
(
    ev => GenericEvent += ev,
    ev => GenericEvent -= ev
);

Hopefully this helps a bit.

辞慾 2025-01-01 10:41:37

FromEvent 扩展方法已重命名为 FromEventPattern,因此执行此操作,101 个示例应该可以工作。

令人困惑的是,为非标准事件创建了新的 FromEvent 重载。他们应该将其命名为 FromEventAction 但他们没有。

The FromEvent extension method has been renamed to FromEventPattern so do this and the 101 samples should work.

The confusing thing is that a new FromEvent overload was created for non-standard events. They should have called it FromEventAction but they didn't.

空名 2025-01-01 10:41:37

A lot of samples from Programming Reactive Extensions and LINQ by Jesse Liberty and Paul Betts book is broken :( :( It's so sad...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文