简体   繁体   English

F#中的IDelegateEvent和IEvent有什么区别?

[英]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. F#为与类型IDelegateEvent兼容的非虚拟实例成员属性赋予特殊状态,生成适当的.NET元数据以使该成员作为.NET事件显示给其他.NET语言。

But the behavior of IDelegateEvent and IEvent appears to be the same with or without the CLIEvent attribute. 但是,无论有没有CLIEvent属性,IDelegateEvent和IEvent的行为都是相同的。

I'm using 1.9.7.8. 我正在使用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继承自IDelegateEvent,但对允许的委托类型施加了额外的约束(强制它返回单元)。 IEvent also inherits from IObservable. IEvent也继承自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. 除非你想要一个具有奇怪类型的事件,否则没有理由依赖基础IDelegateEvent接口而不是更常见的IEvent。

What do you mean when you say that [<CLIEvent>] doesn't make a difference? 当你说[<CLIEvent>]没有什么区别时,你是什么意思? In FSI there is a bug which prevents events from being generated correctly on the type (eg as seen via Type.GetEvents() ), but events are generated correctly when the code is compiled. 在FSI中,有一个错误可以防止在类型上正确生成事件(例如,通过Type.GetEvents()看到),但是在编译代码时会正确生成事件。 Even is FSI you should see the add_ and remove_ methods being generated when you apply the [<CLIEvent>] attribute, though. 即使是FSI,您应该看到在应用[<CLIEvent>]属性时生成的add_和remove_方法。

I think that in some historic versions of F#, you would use IDelegateEvent for standard .NET events and IEvent for F# events. 我认为在F#的一些历史版本中,您可以将IDelegateEvent用于标准.NET事件,将IEvent用于F#事件。 The CLIEvent attribute was added more recently (because the original solution had quite a few issues). 最近添加了CLIEvent属性(因为原始解决方案有很多问题)。 So, I think the IDelegateEvent is very rarely useful now ( kvb mentioned some more specific differences). 所以,我认为IDelegateEvent现在很少有用( kvb提到了一些更具体的差异)。

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... 一个更有趣的区别是IEvent<'T> ,它是一个带有处理程序Handler<'T>的事件(我相信它在F#库中的某个地方定义)和IEvent<'Del, 'T>可以和任何处理程序一起使用,例如IEvent<EventHandler, EventArgs> ,它在.NET库中经常出现......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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