如何在 F# 中创建与 .NET 兼容的事件? [英] How to create .NET-compatible events in F#?

查看:23
本文介绍了如何在 F# 中创建与 .NET 兼容的事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从 F# 类型发布事件,但我希望它被视为来自 C# 或 VB 的事件.似乎正确的方法曾经是 IEvent.create_HandlerEvent,但该函数在最新版本的 F# 中不存在.那么现在正确的做法是什么?

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?

推荐答案

事件不是我的强项,但这个例子似乎适用于 F# 1.9.6.16:

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

另见

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

这篇关于如何在 F# 中创建与 .NET 兼容的事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆