此代码:
// NOTE: IQuote DOES NOT inherit from EventArgs
public interface IQuote// : EventArgs
{
}
class C
{
//NOTE, this is NOT public event EventHandler<IQuote> QuoteChangeEvent;
public delegate void QuoteChangeEvent(IQuote q);
}
var c = new C();
c.OnQuoteChange += ProcessQuoteChange;
public static void ProcessQuoteChange(IQuote q)
{
}
工作正常。现在,我要使用Rx
。这不起作用,即甚至无法编译:
var priceChangedObservable = Observable.FromEvent<QuoteChangeEvent, QuoteChangeEvent>(
(handler) =>
{
QuoteChangeEvent quoteHandler = (e) => handler(e);
},
eh => c.OnQuoteChange += eh,
eh => c.OnQuoteChange -= eh
);
在线(handler) =>
- "Severity Code Description Project File Line Suppression State
Error CS1643 Not all code paths return a value in lambda expression
of type 'Func<Action<QuoteChangeEvent>,
QuoteChangeEvent>' 294 Active"
在handler(e)
错误CS1503参数1:无法从“ IQuote”转换为 'QuoteChangeEvent'296有效
从IObservable
创建delegate
的正确方法是什么?
答案 0 :(得分:1)
使用Observable.FromEvent<TDelegate, TEventArgs>
时,您应该通过<QuoteChangeEvent, IQuote>
:
var priceChangedObservable = Observable.FromEvent<QuoteChangeEvent, IQuote>(
handler =>
{
QuoteChangeEvent quoteHandler = (e) => { handler(e); };
return quoteHandler;
},
eh => c.OnQuoteChange += eh,
eh => c.OnQuoteChange -= eh
);