What is F# equivalent of C# “public event ”

旧城冷巷雨未停 提交于 2019-12-05 00:52:56

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!