我正在Mac上试用f#,但使用非常简单的脚本遇到了问题。我正在关注this tutorial,但是它在results.Descendants [“a”]处中断。

有趣的是,VS将.Descendants识别为有效的结果方法,但是在运行时(仅尝试不带参数的Descendants),我得到:

error CS1061: 'HtmlDocument' does not contain a definition for 'Descendants' and no
extension method 'Descendants' accepting a first argument of type 'HtmlDocument' could
be found (are you missing a using directive or an assembly reference?)

好像FSharp.Data只是部分导入之类的。有趣的是,HtmlDocument有效,但扩展无效。

任何想法如何解决这一问题?

编辑:代码
// Learn more about F# at http://fsharp.org

open System
open FSharp.Data

[<EntryPoint>]
let main argv =
    let results = HtmlDocument.Load("http://www.google.co.uk/search?q=FSharp.Data")
    let links =
        results.Descendants "a"
        |> Seq.choose (fun x ->
               x.TryGetAttribute("href")
               |> Option.map (fun a -> x.InnerText(), a.Value())
        )
    0 // return an integer exit code

Edit2:我手动使用nuget下载了FSharp.Data,并且可以在fsharpi中运行以下脚本。我不知道这和任何Visual Studio有什么区别?
#r "./FSharp.Data.2.4.6/lib/net45/FSharp.Data.dll"
open FSharp.Data

let results = HtmlDocument.Load("http://www.google.co.uk/search?q=FSharp.Data")
let d= results.Descendants "a"

编辑3:在fsharpi和vs中运行之间的区别:
  • fsharpi使用单声道,而VS使用dotnet(这是.net核心2)
  • nuget下载了FSharp.Core 4.0.0.1,而VS使用的是4.3.4

  • 我似乎无法强迫VS使用FSharp.Core 4.0.0.1。即使我添加了特定版本,它仍然使用4.3.4

    最佳答案

    看来问题出在FCore.Sharp Visual Studio默认情况下使用。要使用特定版本,仅通过nuget添加它是不够的,但是必须添加一个

    <FSharpCoreImplicitPackageVersion>4.4.1.18</FSharpCoreImplicitPackageVersion>
    

    到项目配置。这样看来代码可以工作,但是调试器(监视窗口)和即时窗口仍然无法识别Descendants方法。我不知道那怎么可能。我假设VS中存在某种错误。

    另一个选择似乎是使用FSharp.Data 3 beta(具有相同的监视/即时问题)

    关于macos - Fsharp.Data,HtmlDocument没有扩展后代,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50505304/

    10-12 23:00