What's the difference between IDelegateEvent and IEvent in F#?
The docs say:
F# gives special status to non-virtual instance member properties compatible with type IDelegateEvent, generating approriate .开发者_开发技巧NET metadata to make the member appear to other .NET languages as a .NET event.
But the behavior of IDelegateEvent and IEvent appears to be the same with or without the CLIEvent attribute.
I'm using 1.9.7.8.
IEvent inherits from IDelegateEvent, but puts an additional constraint on the type of delegate allowed (forcing it to return unit). IEvent also inherits from IObservable. Unless you want to have an event with a weird type, there's no reason to rely on the base IDelegateEvent interface instead of the more common IEvent.
What do you mean when you say that [<CLIEvent>]
doesn't make a difference? In FSI there is a bug which prevents events from being generated correctly on the type (e.g. as seen via Type.GetEvents()
), but events are generated correctly when the code is compiled. Even is FSI you should see the add_ and remove_ methods being generated when you apply the [<CLIEvent>]
attribute, though.
I think that in some historic versions of F#, you would use IDelegateEvent
for standard .NET events and IEvent
for F# events. The CLIEvent
attribute was added more recently (because the original solution had quite a few issues). So, I think the IDelegateEvent
is very rarely useful now (kvb mentioned some more specific differences).
A more interesting difference is between IEvent<'T>
which is an event with handler Handler<'T>
(which is defined somewhere in F# libraries, I believe) and IEvent<'Del, 'T>
which can be used with any handlers, for example IEvent<EventHandler, EventArgs>
which appears quite frequently in .NET libraries...
精彩评论