【问题标题】:Show documentation for function in fsi在 fsi 中显示函数的文档
【发布时间】:2018-12-19 14:33:41
【问题描述】:

有没有办法让fsi 打印函数的文档?

我可以通过将函数评估为值来检索类型,但我想知道 xml 文档是否存在。

【问题讨论】:

    标签: f# f#-interactive


    【解决方案1】:

    如果没有用例,我有 2 条建议。如果这只是关于帮助,请使用 FSX 文件。

    如果您确实想在 FSI 中打印出文档,您可以使用以下代码为成员打印出文档。由于 xml cmets 未存储在 dll 中,因此此代码检查 xml doc 是否存在于 dll 的位置,如果存在则将其加载。

    #r "packages/Newtonsoft.Json/lib/net45/Newtonsoft.Json.dll"
    
    open System.IO
    open System.Xml
    open System.Reflection
    
    let loadXml (path:string) =
      if (File.Exists(path)) then 
        let xml = new XmlDocument()
        xml.Load(path)
        Some xml
      else None
    
    let xmlForMember (maybeXml:XmlDocument option) (mi:MemberInfo) =
      let path = sprintf "M:%s.%s" mi.DeclaringType.FullName mi.Name
      match maybeXml with
      | None -> None
      | Some xml -> xml.SelectSingleNode("//member[starts-with(@name, '" + path + "')]") |> Some
    
    let docFrom (node:XmlNode option) =
      match node with
      | None -> "No docs available"
      | Some n -> n.InnerXml
    

    用法是这样的,但您可以根据需要整理和打包:

    let t = typedefof<Newtonsoft.Json.JsonSerializer>
    let assembly = t.Assembly
    let dllPath = assembly.Location
    printfn "Assembly location: %s" dllPath
    let expectedXmlPath = Path.ChangeExtension(dllPath, ".xml")
    printfn "Expected xml: %s" expectedXmlPath
    let xmlDoc = expectedXmlPath |> loadXml
    let mi = t.GetMember("Create").[0]
    
    let docNode = mi |> xmlForMember xmlDoc
    
    docNode |> docFrom |> printfn "%s"
    

    希望这可以帮助您入门。

    【讨论】:

      猜你喜欢
      • 2011-05-25
      • 1970-01-01
      • 1970-01-01
      • 2020-01-23
      • 1970-01-01
      • 2022-08-16
      • 1970-01-01
      • 2020-01-19
      • 1970-01-01
      相关资源
      最近更新 更多