我正在尝试从F#订阅/取消订阅C#-made(在RabbitMQ客户端库中):
AsyncEventingBasicConsumer.cs
public event AsyncEventHandler<BasicDeliverEventArgs> Received;
AsyncEventHandler.cs
我设法通过以下方式订阅了该活动:
AsyncEventHandler.cs
话虽如此,我想知道为什么下面的代码无法编译:
public delegate Task AsyncEventHandler<in TEvent>(object sender, TEvent @event) where TEvent : EventArgs;
因为我得到以下错误:
let subscribeAsync (channel: IModel) exchange callback =
let consumer = AsyncEventingBasicConsumer(channel)
consumer.add_Received(AsyncEventHandler<BasicDeliverEventArgs>(fun sender args -> Task.CompletedTask))
// ...
我检查了let subscribeAsync (channel: IModel) exchange callback =
let consumer = AsyncEventingBasicConsumer(channel)
consumer.Received.AddHandler(AsyncEventHandler<BasicDeliverEventArgs>(fun sender args -> Task.CompletedTask))
// ...
,但看不到任何有关F#的标准事件类型的引用。
F#将.NET事件公开为Program.fs(10, 14): [FS1091] The event 'Received' has a non-standard type. If this event is declared in another CLI language, you may need to access this event using the explicit add_Received and remove_Received methods for the event. If this event is declared in F#, make the type of the event an instantiation of either 'IDelegateEvent<_>' or 'IEvent<_,_>'.
或the official MS documentation类型的值,具体取决于事件声明和委托类型。仅对于具有某些基本公共结构的事件,才可以执行此操作-当F#无法执行此操作时,它将把事件的基础IEvent
和IDelegateEvent
操作公开为可以直接调用的方法。