【问题标题】:How to view type providers in the assembly如何在程序集中查看类型提供程序
【发布时间】:2019-05-17 03:08:40
【问题描述】:

我正在查看对象资源管理器并试图找出类型提供程序的定义位置/方式 - 我正在查看 FSharp.Data.dll。它显示 CsvFile 和 CsvRow .. 但我找不到 CsvProvider。这是在哪里定义的?我应该只依靠文档来找出给定程序集中的类型提供程序吗?

【问题讨论】:

    标签: f# f#-data


    【解决方案1】:

    FSharp.Data.dllFSharp.Data 的运行时组件。类型提供程序在编译时为您生成类型,之后就不需要了。该 dll 被称为:FSharp.Data.DesignTime.dll

    您可以反编译该 dll,但我认为只查看源代码会更容易:https://github.com/fsharp/FSharp.Data/blob/master/src/Json/JsonProvider.fs

    类型提供程序的作用是注入代码和类型,使您可以方便地导航 JSON。使用dnSpy 之类的工具可以找出实际发生的情况

    所以示例程序

    type Simple = JsonProvider<""" { "name":"John", "age":94 } """>
    let f (s: string) =
      let s = Simple.Parse s
      s.Name
    

    使用dnSpy 将其反编译为 C# 后如下所示:

        public static string f(string s)
        {
            IJsonDocument s2 = (IJsonDocument)JsonDocument.Create(new StringReader(s));
            JsonValueOptionAndPath jsonValueOptionAndPath = JsonRuntime.TryGetPropertyUnpackedWithPath(s2, "name");
            return JsonRuntime.GetNonOptionalValue<string>(jsonValueOptionAndPath.Path, JsonRuntime.ConvertString("", jsonValueOptionAndPath.JsonOpt), jsonValueOptionAndPath.JsonOpt);
        }
    

    所以字符串被解析成IJsonDocument然后s.Name变成了

        JsonValueOptionAndPath jsonValueOptionAndPath = JsonRuntime.TryGetPropertyUnpackedWithPath(s2, "name");
        return JsonRuntime.GetNonOptionalValue<string>(jsonValueOptionAndPath.Path, JsonRuntime.ConvertString("", jsonValueOptionAndPath.JsonOpt), jsonValueOptionAndPath.JsonOpt);
    

    关于的代码不需要FSharp.Data.DesignTime.dll,因此它不包含在构建中。

    【讨论】:

    • 非常感谢您的详细回复。我主要使用 F# 交互并通过 #r 指令引用库。因此,如果我正确理解了您的解释,在 F# 交互使用期间将不会生成 FSharp.Data.DesignTime.dll,因为没有编译操作(或者它将在后台生成,对我们不可见)。而且我们确实需要查看源代码检查给定程序集是否具有类型提供程序。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多