Reputation: 548
Looking at various bits of documentation, the way of defining an event in F# is to do something like
type xyz () =
let e = new Event<T>
member x.something_happened : IEvent<T> = x.Publish
Unfortunately, the type of IEvent is really Miscrosoft.FSharp.Control.IEvent<_>, and it is hence difficult to use from within C#. Some articles suggest adding the CLIEvent attribute to member something_happended above but it seems to make no difference as far as its usability from C# without including the F# library goes.
How do I correctly define an event in F# so I can then add a delegate to it in C# code? Many thanks.
Upvotes: 3
Views: 497
Reputation: 47904
There are two event types in F#, Event<'T>
and Event<'Delegate, 'Args>
. Only the second one is compiled to a .NET event when [<CLIEvent>]
is present. Here's a working example:
type T() =
let e = Event<EventHandler<_>,_>()
[<CLIEvent>]
member x.MyEvent = e.Publish
member x.RaiseMyEvent() = e.Trigger(x, EventArgs.Empty)
In some cases the compiler generates a warning if [<CLIEvent>]
is used with a non-standard event type. I'm not sure why it doesn't raise a warning for your code (perhaps a bug?).
Upvotes: 13