从“非标准”创建Observable .NET事件 [英] Creating an Observable from a "non-standard" .NET event

查看:45
本文介绍了从“非标准”创建Observable .NET事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我首先概述这个问题:

Let me start with a generalization of the question:

我已经做了很多关于Rx的阅读。而且我真的想将我的事件驱动的C#应用​​程序移动到Rx(出于很多原因,你们大多数人可能已经熟悉了)。我的大部分工作都涉及获取现有事件并将其转换为Rx Observable。
我通过一些例子,我得到了"基本"和"基本"。使用Observable.FromEvent和Observable.FromEventPattern进行此转换的版本。当我说"基本"时,我的意思是转换标准
EventHandler或从EventHandler继承的事件。

I have done a lot of reading about Rx. And I REALLY want to move my event-driven C# apps to Rx (for a lot of reasons which most of you are probably already acquainted). So much of my work deals with taking existing events and converting them to Rx Observable. I worked through some of the examples, and I was able to get the "basic" version of this conversion working with Observable.FromEvent and Observable.FromEventPattern. When I say "basic," I mean converting events that are either standard EventHandler or inherit from EventHandler.

然而,在现实世界中,我要处理的很多事件都不是标准的EventHandler事件。所以我的问题是,如何从"非标准"创建一个Observable。事件?

In the real world, however, a lot of the events I have to deal with are NOT standard EventHandler events. So my question is, how do I create an Observable from a "non-standard" event?

以下是一个具体示例:

I在金融交易API中有一个事件(不是我的API,所以我无法改变事件的组成方式):

I have an event in a financial trading API (not my API, so I can't change how the event is composed):

< span style ="white-space:pre"> 公共事件AccountList.AccountDetailsEventHandler AccountDetails;

public event AccountList.AccountDetailsEventHandler AccountDetails;

public delegate void AccountDetailsEventHandler(AccountList.UpdateList poAccounts);

public delegate void AccountDetailsEventHandler(AccountList.UpdateList poAccounts);

我的代码使用如下事件:

My code uses the event as follows:

_accountList.AccountDetails + = new T4.API.AccountList.AccountDetailsEventHandler(moAccount_AccountDetails);

_accountList.AccountDetails += new T4.API.AccountList.AccountDetailsEventHandler(moAccount_AccountDetails);

private void moAccounts_AccountDetails(T4.API.AccountList.UpdateList poAccounts)

private void moAccounts_AccountDetails(T4.API.AccountList.UpdateList poAccounts)

{

// ...事件处理程序的代码在这里...

// ... code for event handler goes here...

}

因此,不要让事件触发并调用事件处理程序"moAccounts_AccountDetails",我想创建一个Observable。我尝试了以下变体:

So rather than have the event fire and call the event handler "moAccounts_AccountDetails," I want to create an Observable. I have tried variations of the following:

var details1 = Observable.FromEventPattern< AccountList.AccountDetailsEventHandler>(

var details1 = Observable.FromEventPattern<AccountList.AccountDetailsEventHandler>(

ev => _accountList.AccountDetails + = ev,

ev => _accountList.AccountDetails += ev,

ev => _accountList.AccountDetails - = ev);

ev => _accountList.AccountDetails -= ev);

var details2 = Observable.FromEventPattern(

var details2 = Observable.FromEventPattern(

handler =>(sender,e)=> handler(sender) ,e),

handler => (sender, e) => handler(sender, e),

h => _accountList.AccountDetails + = h,

h => _accountList.AccountDetails += h,

h => _accountList.AccountDetails - = h);

h => _accountList.AccountDetails -= h);

var details3 = Observable.FromEvent(

var details3 = Observable.FromEvent(

h => _accountList.AccountDetails + = h,

h => _accountList.AccountDetails += h,

h => _accountList.AccountD etails - = h);

h => _accountList.AccountDetails -= h);

我想提前道歉,因为他可能会犯一些初学者的错误。但是我已经看了很多,并且利用"非标准"的例子的数量是多少。用于创建Rx Observables的事件几乎不存在。

I want to apologize ahead of time for making some probably beginner mistakes. But I have looked all over, and the number of examples of utilizing "non-standard" events to create Rx Observables is almost non-existent.

您能否就正确处理"非标准"的方式给我一些指导。从这些"非标准"事件中创建事件并创建Observable。事件?在这种情况下,例如,我必须处理一个API,其中事件被设置为.NET
事件,其代理不是标准委托(不是Action委托,不是EventHandler,不是传递EventArgs等。)

Can you give me some guidance on the correct way to deal with "non-standard" events and creating Observables from these "non-standard" events? In this case, for example, I have to deal with an API in which the event is set up as a .NET event with a delegate that is not a standard delegate (not an Action delegate, not an EventHandler, not passing EventArgs, etc).

推荐答案

哦亲爱的,你迈克尔。旧的API看起来像是"挑战的机会"。 : - )

Oh dear poor you Michael. The old API looks like "an opportunity for a challenge" :-)

我能够在LinqPad中重现问题并找到你正在寻找的Overload

I was able to reproduce the problem in LinqPad and find the Overload you are looking for

void Main()
{
	var _accountList = new AccountList();
	_accountList.AccountDetails += moAccounts_AccountDetails;
	Observable.FromEvent<AccountList.AccountDetailsEventHandler,AccountList.UpdateList>(
		action=>
		{
			AccountList.AccountDetailsEventHandler handler = delegate(AccountList.UpdateList poAccounts){action(poAccounts);};
			return handler;
		},
		h=>_accountList.AccountDetails+=h, 
		h=>_accountList.AccountDetails-=h).Dump("FromEvent");
	_accountList.RaiseEvent("Lee");
}

// Define other methods and classes here
public class AccountList
{
	public delegate void AccountDetailsEventHandler(AccountList.UpdateList poAccounts);
	
	public event AccountList.AccountDetailsEventHandler AccountDetails;	
	
	public void RaiseEvent(string id)
	{
		var handler = AccountDetails;
		if(handler!=null) handler(new UpdateList(id));
	}
	
	
	public class UpdateList
	{
		public UpdateList(string id)
		{
			Id = id;
		}
		public string Id { get; private set; }
	}
}


private void moAccounts_AccountDetails(AccountList.UpdateList poAccounts)
{
	poAccounts.Dump("moAccounts_AccountDetails");
}

关键是你希望能够让穷人的旧Rx / Linq / C#有机会搞清楚你一直留下的烂摊子。您可以通过从Action< TEventArgs>转换它来实现此转换。代表您的活动支持。我使用
代码执行此操作

The key is that you want to be able to give poor old Rx/Linq/C# a chance at figuring out the mess you have been left with. You do this by giving it a conversion from Action<TEventArgs> to the Delegate your event supports. I have done this with the following code

action=>
  {
	AccountList.AccountDetailsEventHandler handler = delegate(AccountList.UpdateList poAccounts){action(poAccounts);};
	return handler;
  },

即。我创建了一个匹配委托的处理程序,但他所做的只是委托对Rx提供的Action的调用。

i.e. I create a handler that matches the delegate, but all he does is delegate the call to the Rx provided Action.

我希望这会有所帮助。

另一种选择是做这篇文章中的人做的事情,只是创建一个扩展方法利用Observable.Create

Another option would be to do what the guys in this post did, and just create an extension method leveraging Observable.Create

http://social.msdn.microsoft.com/Forums/en-US/rx/thread/4b3c33e9-e9cb-48fb-8189-dc75354e33ce

http://social.msdn.microsoft.com/Forums/en-US/rx/thread/4b3c33e9-e9cb-48fb-8189-dc75354e33ce

Lee Campbell

Lee Campbell


这篇关于从“非标准”创建Observable .NET事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆