gpt4 book ai didi

f# - 在可移植类库中使用 F# JsonProvider 失败

转载 作者:行者123 更新时间:2023-12-02 09:01:07 25 4
gpt4 key购买 nike

我正在尝试使用 JsonProvider,但在调用其函数时收到以下错误:

System.TypeInitializationException was unhandled
Message: An unhandled exception of type 'System.TypeInitializationException' occurred in PortableLibrary1.dll
Additional information: The type initializer for '<StartupCode$PortableLibrary1>.$PortableLibrary1' threw an exception.

我有一个基本的控制台应用程序,如下所示:

module Pinit =
open FSharp.Data

type JsonT = JsonProvider<"""..\myFile.json""">
let doc = JsonT.Load("""..\nyFile.json""")
let result = doc.GeneratedAt

[<EntryPoint>]
let main argv =
printfn "%A" Pinit.doc.GeneratedAt
0

当在 ConsoleApplication 中运行时,一切都会按预期工作。如果我创建一个 F# 可移植类库,如下所示:

module Pinit =
open FSharp.Data

type JsonT = JsonProvider<"""..\myFile.json""">
let doc = JsonT.Load("""..\nyFile.json""")
let result = doc.GeneratedAt

创建另一个控制台应用程序并引用该可移植类库并调用代码,如下所示:

open PortableLibrary1

[<EntryPoint>]
let main argv =
printfn "%A" Pinit.result
0

当我运行该程序时,它会生成上面定义的异常:

我怀疑这是因为 FSharp.Core 的版本,但想知道我是否做错了什么,或者是否有办法让它工作?

版本:

-- ConsoleApplication --
FSharp.Core = 4.3.1.0

-- PortableClassLibrary --
FSharp.Core = 3.3.1.0
FSharp.Data = NuGet Version: 2.0.0

最佳答案

let 绑定(bind)被编译为静态成员,而在 .NET 中,当静态初始化程序中出现异常时,真正的异常会被伪装。

如果您通过更改为 let result() = doc.GenerateAtprintfn "%A"(Pinit.result()) 将其转换为函数调用,您会得到真正的异常:

Only web locations are supported

可移植配置文件不支持访问文件系统。因此,在 PCL 内,您可以拥有 Web URL 或从嵌入式资源手动加载。请参阅此处的示例:https://github.com/ovatsus/Apps/blob/master/Trains/Stations.fs#L65 。或者您可以在控制台项目中使用 File.ReadAllText 加载文件并将其传递给 PCL。

这是一个可移植配置文件(旧版)项目,即配置文件 47(和 FSharp.Core 2.3.6.0)。然后我还使用 Portable Profile 项目进行了测试,即 Profile 7(和 FSharp.Core 3.3),并注意到它无法正常工作,而是给出了此异常

Method not found: 'FSharp.Data.Runtime.JsonValueOptionAndPath FSharp.Data.Runtime.JsonRuntime.TryGetPropertyUnpackedWithPath(FSharp.Data.Runtime.IJsonDocument, System.String)'.

我创建了一个 github 问题来跟踪:https://github.com/fsharp/FSharp.Data/issues/521

关于f# - 在可移植类库中使用 F# JsonProvider 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22364717/

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