问题描述
反思支持当前已加入F#,但它不工作的措施类型。是否有可能使用反射在F#中的度量类型?
我读过的。那是2008年,但如果你在ILDASM检查像波纹管一些代码,你不能看到测量
单位任何事情。
Support for reflection has been currently added into F#, but it is not working for measure types. Is it possible to use reflection in F# for measure types?I've read this. It was for 2008, but if you check some code like bellow in ildasm you cannot see anything about Units of Measure
.
// Learn more about F# at http://fsharp.net
[<Measure>] type m
[<Measure>] type cm
let CalculateVelocity(length:float<m> ,time:float<cm>) =
length / time
的反汇编输出:
.method public static float64 CalculateVelocity(float64 length,
float64 time) cil managed
{
// Code size 5 (0x5)
.maxstack 4
IL_0000: nop
IL_0001: ldarg.0
IL_0002: ldarg.1
IL_0003: div
IL_0004: ret
} // end of method Program::CalculateVelocity
因此,有不能在F#被反射出头。它是真的还是假的?
查看注释:单位居然没有得到见于全部由CLR ......在文章
So there are somethings that cannot be reflected in F#. Is it true or not?see the comment : Units actually don't get seen at all by the CLR ... in the article.
推荐答案
正如其他人已经指出的那样,当你需要得到有关编译F#类型的一些信息,你可以使用标准的.NET反射(的System.Reflection
)和F#反射提供有关识别联合,记录等信息( Microsoft.FSharp.Reflection
)。
As others already pointed out, when you need to get some information about compiled F# types, you can use standard .NET reflection (System.Reflection
) and F# reflection which provides information about discriminated unions, records, etc. (Microsoft.FSharp.Reflection
).
可惜的是,有关的信息度量单位的不能使用任何这两个API的访问,因为它们是在编译期间只检查,在运行时并不实际存在(它们不能以任何方式在CLR来表示)。这意味着你将永远无法寻找是否如装箱的浮点值具有测量某单位...
Unfortunatelly, information about units of measure cannot be accessed using any of these two APIs, because they are checked only during the compilation and do not actually exist at runtime (they cannot be represented in the CLR in any way). This means that you'll never be able to find out whether e.g. a boxed floating point value has some unit of measure...
您可以得到的部分的有关使用计量单位信息从F#PowerPack的元数据
命名空间。例如,下面的打印的富
是一个单位:
You can get some information about units of measure using Metadata
namespace from F# PowerPack. For example, the following prints that foo
is a unit:
namespace App
open System.Reflection
open Microsoft.FSharp.Metadata
[<Measure>]
type foo
module Main =
let asm = FSharpAssembly.FromAssembly(Assembly.GetExecutingAssembly())
for ent in asm.Entities do
if ent.IsMeasure then
printfn "%s is measure" ent.DisplayName
这读取一些二进制文件的元数据,在编译的文件编译店(这样你可以看到单元时你引用其他F#库),所以你应该能够看到有关情报F#库的公共API。
This reads some binary metadata that the compiler stores in compiled files (so that you can see units when you reference other F# libraries), so you should be able to see informaiton about public API of F# libraries.
这篇关于反射的度量F#机组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!