简体   繁体   中英

Consume C# event from F#

Followed Functional Reactive Programming tutorial, where stream of events ( Observable ) is directly created from System.Timers.Timer.Elapsed event.

 let timer = new System.Timers.Timer(float 1)
 let observable = timer.Elapsed  
 observable 
 |> Observable.subscribe ...

Have class defined in C#

public delegate void OnMessageDelegate(Message message);

public class Notifier : INotifier
{
   public event OnMessageDelegate OnMessage;

   public void Notify(Message message) 
   => OnMessage?.Invoke(message);
}

Referenced corresponding .dll into F# project and trying to create Observable from Notifier.OnMessage event

 let Start (notifier : Namespace.Notifier)  = 
    notifier.OnMessage      
    |> Observable.subscribe (fun ms -> ...
    0

Getting error message

The event OnMessage 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_OnMessage and remove_OnMessage 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 error message is self descriptive, though I aim to create stream from event and not to subscribe on it via add_OnMessage method.

It appears that F# expects the delegate type for events to be System.EventHandler or a specialization of System.EventHandler<'TEventArgs> .

Try changing the type of OnMessage from OnMessageDelegate to EventHandler<Message> .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM