How to create .NET-compatible events in F#?

人走茶凉 提交于 2019-12-18 21:55:10

问题


I am trying to publish an event from an F# type, but I want it to be seen as an event from C# or VB. It seems that the correct way to do it used to be IEvent.create_HandlerEvent, but this function doesn't exist in the newest version of F#. So what is the correct way to do it now?


回答1:


Events are not my forte, but this example seems to work on F# 1.9.6.16:

namespace EventExample
open System
type MyEventArgs(msg:string) =
    inherit EventArgs()
    member this.Message = msg

type MyEventDelegate = delegate of obj * MyEventArgs -> unit

type Foo() = 
    let ev = new Event<MyEventDelegate, MyEventArgs>()

    member this.Ping(msg) =
        ev.Trigger(this, new MyEventArgs(msg))

    [<CLIEvent>]
    member this.GotPinged = ev.Publish

See also

http://cs.hubfs.net/forums/thread/10555.aspx



来源:https://stackoverflow.com/questions/904235/how-to-create-net-compatible-events-in-f

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