[英]How to write an Event member for an F# interface?
I am new to F#.我是 F# 的新手。 In C#, I have an interface defined as:在 C# 中,我有一个接口定义为:
public interface IMenuEvents
{
event Action SaveEvent;
event EventHandler<ParagraphReadyEventArgs> ParagraphReadyEvent;
....
}
where在哪里
public class ParagraphReadyEventArgs : System.EventArgs
{
public ParagraphReadyEventArgs(string paragraph_title)
{
ParagraphTitle = paragraph_title;
}
public string ParagraphTitle { get; }
}
How is the interface IMenuEvents written in F# ?接口 IMenuEvents 是如何用 F# 编写的?
Thank you for any help.感谢您的任何帮助。
Given declaration translates to给定的声明转化为
open System
type ParagraphReadyEventArgs(paragraphTitle: string) =
inherit EventArgs()
member val ParagraphTitle = paragraphTitle
type IMenuEvents =
[<CLIEvent>]
abstract member SaveEvent : IDelegateEvent<EventHandler>
[<CLIEvent>]
abstract member ParagraphReadyEvent : IDelegateEvent<EventHandler<ParagraphReadyEventArgs>>
Key difference is that EventHandler
wrapped inside IDelegateEvent<T>
主要区别在于EventHandler
包裹在IDelegateEvent<T>
Behind the scenes you get same interface that you could get with C#.在幕后,您将获得与 C# 相同的界面。 Decompilation 反编译
I don't understand why F# chose to make things that complicated yet.我不明白为什么 F# 选择让事情变得复杂。 Hope somebody have an answer for this question希望有人能回答这个问题
Few words about original design: It's recommended for all events to have type EventHandler
or EventHandler<TEventArgs>
, don't use custom event types.关于原始设计的几句话:建议所有事件都具有类型EventHandler
或EventHandler<TEventArgs>
,不要使用自定义事件类型。 If you concern about memory allocation from EventArgs
, then use EventArgs.Empty
instead of new EventArgs()
如果您担心EventArgs
内存分配,请使用EventArgs.Empty
而不是new EventArgs()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.