4

我正在尝试从这篇文章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# 到 F# 类的转换中也提出了这个问题——“公共事件”仍然没有解决方案

解决了。感谢叶花环

type MyFirstEventHandler = delegate of string -> unit
type MyFsComComponent () =    
    let my_event = new DelegateEvent<MyFirstEventHandler> ()

    [<CLIEvent>]
    member x.MyFirstEvent = my_event.Publish

成功了。

4

1 回答 1

6

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
于 2013-06-18T13:43:39.073 回答