计量单位如何表示?
我刚刚开始使用 F#,想知道度量单位:
我知道它们仅在编译时存在,Reflector 证实了这一点:创建一个不是别名的度量单位会创建一个带有 MeasureAttribute 的类> 属性,但除此之外,float
表示为纯 System.Double
。并且类型、构造函数参数或属性上没有属性。
到目前为止我明白了。但是当我从另一个项目引用编译后的程序集时,它似乎知道所有有关度量单位的信息,包括别名。它如何获得这些信息?他们在议会的什么地方?
I'm just starting with F# and am wondering about units of measure:
I understand they are only present at compile time and Reflector confirms this: creating a unit of measure that is not an alias creates a class with MeasureAttribute
attribute, but other than that, float<km/h>
is represented as plain System.Double
. And there are no attributes on the type, constructor parameter or properties.
So far I understand. But when I reference the compiled assembly from another project, it seems to know all about the units of measure, including aliases. How does it get that information? Where in the assembly are they?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
F# 将“额外类型信息”存储在已编译程序集中的资源中,并且 F# 编译器知道如何读取该资源。因此,虽然可区分联合只是编译为类,并且度量单位被擦除为双精度,但程序集中的资源中存在额外的 F# 特定类型信息,因此当 F# 编译器读取它时,它可以重新构建额外的“F# 元数据”。
PowerPack 有一个元数据读取器,可让您以编程方式访问它。
F# stores 'extra type information' in a resource in the compiled assembly, and the F# compiler knows how to read that resource. So whereas a discriminated union is just compiled into, say, a class, and a unit of measure is erased into a double, there's extra F#-specific type info in a resource in the assembly so that when the F# compiler reads it, it can re-construct the extra "F# metadata".
The PowerPack has a metadata reader that lets you access it programmatically.