何时使用 Observable.FromEventPattern 而不是 Observable.FromEvent?

发布于 2024-11-17 02:27:47 字数 897 浏览 2 评论 0 原文

我们有一个客户端调用 TIBCO EMS 队列,并像这样连接事件:

var msgConsumer = _session.CreateConsumer(responseQueue);
var response = Observable
    .FromEvent<EMSMessageHandler, EMSMessageEventArgs> (
        h => msgConsumer.MessageHandler += h,
        h => msgConsumer.MessageHandler -= h)
    .Where(arg => arg.Message.CorrelationID == message.MessageID);

当我调用 response.Subscribe(...) 时,我收到 System.ArgumentException“绑定到目标时出错”方法。”

我可以让它与 Observable.FromEventPattern(msgConsumer, "MessageHandler") 一起使用,但它会将事件作为字符串,但不那么干净。

另外,我有 IObservable> 而不是 IObservable

我想了解的是:什么时候应该使用 FromEvent 超过 FromEventPattern?看起来有点反复试验。

We've got a client calling off to a TIBCO EMS queue and are wiring up the events like this:

var msgConsumer = _session.CreateConsumer(responseQueue);
var response = Observable
    .FromEvent<EMSMessageHandler, EMSMessageEventArgs> (
        h => msgConsumer.MessageHandler += h,
        h => msgConsumer.MessageHandler -= h)
    .Where(arg => arg.Message.CorrelationID == message.MessageID);

When I call response.Subscribe(...) I get System.ArgumentException "Error binding to target method."

I can make it work with Observable.FromEventPattern<EMSMessageEventArgs>(msgConsumer, "MessageHandler") but then it's got the event as a string and just not as clean.

Also I have IObservable<EventPattern<EMSMessageEventArgs>> rather than IObservable<EMSMessageEventArgs>

What I'd like to understand is: when should I use FromEvent over FromEventPattern? It seems a bit trial and error.

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

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

发布评论

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

评论(3

只是偏爱你 2024-11-24 02:27:47

“对结构上的事件使用 FromEvent 看起来不像 .NET 事件模式(即不基于发送者、事件参数),而对基于模式的事件使用 FromEventPattern。” - Bart De Smet(Rx 团队) )

"Use FromEvent for events structurally don't look like a .NET event pattern (i.e. not based on sender, event args), and use FromEventPattern for the pattern-based ones." - Bart De Smet (Rx team)

谜兔 2024-11-24 02:27:47

为了进一步详细说明这一点,您通常可以根据您要观察的类中使用的事件类型来确定何时选择 FromEventFromEventPattern 之一。使用 FromEventPattern 当您的事件属于 EventHandler 类型或通用 EventHandler 类型时。使用 FromEvent 当您使用自定义的非通用事件处理程序类型时。
以下示例直接取自 Rx wiki,其中有很多很好的示例 - 准确地说,其中有 101 个

FromEventPattern(使用通用事件处理程序)

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

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

FromEvent:

class ObserveEvent_NonGeneric
{
    public class SomeEventArgs : EventArgs { }
    public delegate void SomeNonGenericEventHandler(object sender, SomeEventArgs e);
    public static event SomeNonGenericEventHandler NonGenericEvent;

    static void Main()
    {
        // To consume NonGenericEvent as an IObservable, first inspect the type of EventArgs used in the second parameter of the delegate.
        // In this case, it is SomeEventArgs.  Then, use as shown below.
        IObservable<IEvent<SomeEventArgs>> eventAsObservable = Observable.FromEvent(
            (EventHandler<SomeEventArgs> ev) => new SomeNonGenericEventHandler(ev), 
            ev => NonGenericEvent += ev,
            ev => NonGenericEvent -= ev);
    }
}

To elaborate on this a bit further, you can typically determine when to choose one of the FromEvent vs FromEventPattern by the type of event used in the class you're trying to observe. Use FromEventPattern when your event is of type EventHandler or the generic EventHandler<T>. Use FromEvent when you're using a custom, non-generic event handler type.
The following examples are lifted directly from the Rx wiki, which has lots of good examples- 101 of them to be exact.

FromEventPattern (with generic EventHandler):

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

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

FromEvent:

class ObserveEvent_NonGeneric
{
    public class SomeEventArgs : EventArgs { }
    public delegate void SomeNonGenericEventHandler(object sender, SomeEventArgs e);
    public static event SomeNonGenericEventHandler NonGenericEvent;

    static void Main()
    {
        // To consume NonGenericEvent as an IObservable, first inspect the type of EventArgs used in the second parameter of the delegate.
        // In this case, it is SomeEventArgs.  Then, use as shown below.
        IObservable<IEvent<SomeEventArgs>> eventAsObservable = Observable.FromEvent(
            (EventHandler<SomeEventArgs> ev) => new SomeNonGenericEventHandler(ev), 
            ev => NonGenericEvent += ev,
            ev => NonGenericEvent -= ev);
    }
}
千と千尋 2024-11-24 02:27:47

显然新语法发生了一些变化。当前语法如下:

// get events as observable 
var events = Observable.FromEvent<MouseButtonEventHandler, MouseEventArgs>(
    handler => MouseUp += handler, handler => MouseUp -= handler);

// subscribe to events
var subscription = events.Subscribe(args => OnMouseUp()));

// always dispose subscriptions! 
subscription.Dispose(); 

Apparently the new syntax has changed a bit. The current syntax is the following:

// get events as observable 
var events = Observable.FromEvent<MouseButtonEventHandler, MouseEventArgs>(
    handler => MouseUp += handler, handler => MouseUp -= handler);

// subscribe to events
var subscription = events.Subscribe(args => OnMouseUp()));

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