Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create .NET-compatible events in F#?

Tags:

.net

events

f#

I am trying to publish an event from an F# type, but I want it to be seen as an event from C# or VB. It seems that the correct way to do it used to be IEvent.create_HandlerEvent, but this function doesn't exist in the newest version of F#. So what is the correct way to do it now?

like image 804
Michał Bendowski Avatar asked May 24 '09 17:05

Michał Bendowski


1 Answers

Events are not my forte, but this example seems to work on F# 1.9.6.16:

namespace EventExample
open System
type MyEventArgs(msg:string) =
    inherit EventArgs()
    member this.Message = msg

type MyEventDelegate = delegate of obj * MyEventArgs -> unit

type Foo() = 
    let ev = new Event<MyEventDelegate, MyEventArgs>()

    member this.Ping(msg) =
        ev.Trigger(this, new MyEventArgs(msg))

    [<CLIEvent>]
    member this.GotPinged = ev.Publish

See also

http://cs.hubfs.net/forums/thread/10555.aspx

like image 104
Brian Avatar answered Sep 21 '22 21:09

Brian