gpt4 book ai didi

c# - 如何在 F# 代码中使用 Microsoft Coyote - 父类型中 protected 嵌套属性类型

转载 作者:行者123 更新时间:2023-12-04 00:19:26 24 4
gpt4 key购买 nike

F# 和 C# 之间的语言差异是否阻止了从 F# 代码中使用 Microsoft Coyote?

因为 OnEventDoActionAttribute 是在继承类型 Microsoft.Coyote.Actors.Actor 中定义的,带有 protected 访问修饰符,看起来它仍然可以在继承的 Actor 类型中访问在 C# 中,但在 F# 中则不然。

Hello world 示例转换为 F#:

type SetupEvent(serverId : ActorId) =
inherit Event()
member this.ServerId = serverId

type PingEvent(callerId : ActorId) =
inherit Event()
member this.Caller = callerId

type PongEvent() =
inherit Event()

// both attribute spec fail
//[<Microsoft.Coyote.Actors.Actor.OnEventDoAction(typeof<PingEvent>, "HandlePing")>] // Error: The type 'OnEventDoActionAttribute' is not accessible from this code location
[<OnEventDoAction(typeof<PingEvent>, "HandlePing")>] // Error: The type 'OnEventDoAction' is not defined
type Server() =
inherit Actor()

member this.HandlePing(e : Event) =
let ping = e :?> PingEvent
printfn "Server handling ping"
printfn "Server sending pong back to caller"
this.SendEvent(ping.Caller, new PongEvent());

// both attribute spec fail
//[<Microsoft.Coyote.Actors.Actor.OnEventDoAction(typeof<PongEvent>, "HandlePong")>] // Error: The type 'OnEventDoActionAttribute' is not accessible from this code location
[<OnEventDoAction(typeof<PongEvent>, "HandlePong")>] // Error: The type 'OnEventDoAction' is not defined
type Client() =

inherit Actor()

let mutable serverId : ActorId = null

override this.OnInitializeAsync(initialEvent : Event) : System.Threading.Tasks.Task =
printfn "%A initializing" this.Id
serverId <- (initialEvent :?> SetupEvent).ServerId
printfn "%A sending ping event to server" this.Id
this.SendEvent(serverId, new PingEvent(this.Id))
base.OnInitializeAsync(initialEvent)

member this.HandlePong() =
printfn "%A received pong event" this.Id

[<Test>]
let Execute (runtime : IActorRuntime) =
let serverId = runtime.CreateActor(typeof<Server>)
runtime.CreateActor(typeof<Client>, new SetupEvent(serverId)) |> ignore
runtime.CreateActor(typeof<Client>, new SetupEvent(serverId)) |> ignore
runtime.CreateActor(typeof<Client>, new SetupEvent(serverId)) |> ignore

let runtime = RuntimeFactory.Create()
Execute(runtime) |> ignore
Console.ReadLine() |> ignore

不确定如何解决此问题。

直接试用代码的LINQPad文档URI:http://share.linqpad.net/a9rif7.linq

最佳答案

是的,它是为了减少作用域污染,并确保只有当您在 Visual Studio 中的有效位置使用这些类型时,您才能获得这些类型的智能感知。不过构建 F# 示例的想法很酷...

关于c# - 如何在 F# 代码中使用 Microsoft Coyote - 父类型中 protected 嵌套属性类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61694051/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com