问题
I'm trying to translate to F# code from this article http://www.codeproject.com/Articles/35532/C-COM-Object-for-Use-In-JavaScript-HTML-Including
I've stumbled on this lines:
public delegate void MyFirstEventHandler(string args);
public event MyFirstEventHandler MyFirstEvent;
I've tried to translate this in F# as:
type MyFirstEventHandler = delegate of string -> unit
type MyFsComComponent () =
let my_event = new Event<MyFirstEventHandler,string> ()
[<CLIEvent>]
member x.MyFirstEvent = my_event.Publish
And I get: 'MyFirstEventHandler' has a non-standard delegate
I can compile with:
type MyFirstEventHandler = delegate of obj*string -> unit
But this is not what needed in COM-control.
This question is also raised in C# to F# class transition - "public event" still did not have solution
SOLVED. Thanks to Leaf Garland
type MyFirstEventHandler = delegate of string -> unit
type MyFsComComponent () =
let my_event = new DelegateEvent<MyFirstEventHandler> ()
[<CLIEvent>]
member x.MyFirstEvent = my_event.Publish
does the trick.
回答1:
The F# Event class expects the argument types as generic parameters, not the delegate type. It also expects that the delegate is a standard type (e.g. something like obj*eventargs->unit
). For arbitrary delegates use DelegateEvent.
type MyFirstEventHandler = delegate of string -> unit
type MyFsComComponent () =
let my_event = new DelegateEvent<MyFirstEventHandler>()
[<CLIEvent>]
member x.MyFirstEvent = my_event.Publish
来源:https://stackoverflow.com/questions/17169757/what-is-f-equivalent-of-c-sharp-public-event