我正在尝试从本文http://www.codeproject.com/Articles/35532/C-COM-Object-for-Use-In-JavaScript-HTML-Including
翻译成F#代码我偶然发现了这条线:
public delegate void MyFirstEventHandler(string args);
public event MyFirstEventHandler MyFirstEvent;
我试图在F#中将其翻译为:
type MyFirstEventHandler = delegate of string -> unit
type MyFsComComponent () =
let my_event = new Event<MyFirstEventHandler,string> ()
[<CLIEvent>]
member x.MyFirstEvent = my_event.Publish
我得到:'MyFirstEventHandler'有一个非标准委托
我可以编译:
type MyFirstEventHandler = delegate of obj*string -> unit
但这不是COM控制所需要的。
C# to F# class transition中也提出了这个问题 - “公共事件”仍然没有解决方案
解决。感谢Leaf Garland
type MyFirstEventHandler = delegate of string -> unit
type MyFsComComponent () =
let my_event = new DelegateEvent<MyFirstEventHandler> ()
[<CLIEvent>]
member x.MyFirstEvent = my_event.Publish
诀窍。
答案 0 :(得分:5)
F# Event类期望参数类型为通用参数,而不是委托类型。它还期望委托是标准类型(例如obj*eventargs->unit
)。对于任意委托,请使用DelegateEvent。
type MyFirstEventHandler = delegate of string -> unit
type MyFsComComponent () =
let my_event = new DelegateEvent<MyFirstEventHandler>()
[<CLIEvent>]
member x.MyFirstEvent = my_event.Publish