如何在程序集中查看类型提供程序

时间:2019-05-17 03:08:41

标签: f# f#-data

我正在浏览对象浏览器,试图找出类型提供程序的定义位置/方式-我正在浏览FSharp.Data.dll。它显示CsvFile和CsvRow ..但是我找不到CsvProvider。在哪里定义?我是否应该仅依靠文档来了解给定程序集中的类型提供程序?

1 个答案:

答案 0 :(得分:2)

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,因此它不包含在构建中。