Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Consume C# event from F#

Tags:

c#

events

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.

like image 781
tchelidze Avatar asked Jun 24 '16 08:06

tchelidze


1 Answers

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>.

like image 189
ildjarn Avatar answered Oct 31 '22 05:10

ildjarn