如何在F#中获取模块的类型

编程入门 行业动态 更新时间:2024-10-23 18:30:06
本文介绍了如何在F#中获取模块的类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

如何获取模块的"System.Type"?

How to get 'System.Type' of the module?

例如模块:

module Foo = let bar = 1

这不起作用:

printfn "%s" typeof<Foo>.Name

错误是:

The type 'Foo' is not defined

推荐答案

拥有moduleof运算符肯定是一件好事...由于没有一个运算符,最简单的方法可能是使用F#PowerPack中的元数据库:

It would certainly be nice to have a moduleof operator... Since there's not one, the easiest way to do what you want is probably to use the Metadata library in the F# PowerPack:

#r "FSharp.PowerPack.Metadata.dll" open Microsoft.FSharp.Metadata // get .NET assembly by filename or other means let asm = ... let fasm = FSharpAssembly.FromAssembly asm let t = fasm.GetEntity("Foo").ReflectionType

不幸的是,这不适用于动态程序集(例如通过F#Interactive生成的程序集).您可以使用普通的System.Reflection调用执行类似的操作,但这更多地取决于对模块所采用的编译形式有很好的了解.

Unfortunately, this won't work with dynamic assemblies (such as those generated via F# Interactive). You can do something similar using vanilla System.Reflection calls, but that's more dependent on having a good understanding of the compiled form that your module takes.

更多推荐

如何在F#中获取模块的类型

本文发布于:2023-07-30 15:15:43,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1250545.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:模块   类型   如何在

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!