【发布时间】:2019-05-17 03:08:40
【问题描述】:
我正在查看对象资源管理器并试图找出类型提供程序的定义位置/方式 - 我正在查看 FSharp.Data.dll。它显示 CsvFile 和 CsvRow .. 但我找不到 CsvProvider。这是在哪里定义的?我应该只依靠文档来找出给定程序集中的类型提供程序吗?
【问题讨论】:
我正在查看对象资源管理器并试图找出类型提供程序的定义位置/方式 - 我正在查看 FSharp.Data.dll。它显示 CsvFile 和 CsvRow .. 但我找不到 CsvProvider。这是在哪里定义的?我应该只依靠文档来找出给定程序集中的类型提供程序吗?
【问题讨论】:
FSharp.Data.dll 是FSharp.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,因此它不包含在构建中。
【讨论】: