gpt4 book ai didi

f# - 你能在 F# 中列出命名空间或模块的内容吗

转载 作者:行者123 更新时间:2023-12-02 06:56:18 30 4
gpt4 key购买 nike

类似于 this question about clojure ,是否可以在 F# 中列出命名空间或模块的内容?当我为加载的 DLL 打开命名空间时,我没有收到命名空间不存在的错误,但是当我尝试使用记录的函数时,我看到命名空间不存在的错误。

我正在寻找一种列出值和方法而不依赖于 IDE 的编程方式。例如,如果我加载了 F# repl,我正在寻找类似于:

> #r "mylib.DLL"
> open MyLib.Math
> list-namespace-content MyLib.Math;;
val it : string = """
MyLib.Math.Add : int -> int -> int
MyLib.Math.TryDivide : int -> int -> int option
MyLib.Math.Pi : float
"""

最佳答案

据我所知,没有这样的函数可以做到这一点(我看过 FSharpReflectionExtensions 模块),但您可以自己编写一个。所有的构建 block 都在那里。

看起来很奇怪,命名空间并不是 F#、C# 和 Visual Basic .NET 都使用的 .NET 平台的一部分。在 IL 级别,类型只是 identified by name, culture, assembly, etc.命名空间只是作为构成类型名称的字符串的第一部分出现。

但是,给定程序集,您可以列出其所有类型或所有公共(public)类型。下面是后者的一个例子,给定我最近编写的一个 F# 程序集来做网球 kata:

> open System.Reflection;;
> let a = Assembly.LoadFrom @"<path>\Ploeh.Katas.Tennis.PropertyBased.dll";;

val a : Assembly =
Ploeh.Katas.Tennis.PropertyBased, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null

> let ts = a.GetExportedTypes();;

val ts : System.Type [] =
[|Ploeh.Katas.PropertyBased.TennisProperties;
Ploeh.Katas.PropertyBased.Tennis; Ploeh.Katas.PropertyBased.Tennis+Player;
Ploeh.Katas.PropertyBased.Tennis+Player+Tags;
Ploeh.Katas.PropertyBased.Tennis+Point;
Ploeh.Katas.PropertyBased.Tennis+Point+Tags;
Ploeh.Katas.PropertyBased.Tennis+PointsData;
Ploeh.Katas.PropertyBased.Tennis+FortyData;
Ploeh.Katas.PropertyBased.Tennis+Score;
Ploeh.Katas.PropertyBased.Tennis+Score+Tags;
Ploeh.Katas.PropertyBased.Tennis+Score+Points;
Ploeh.Katas.PropertyBased.Tennis+Score+Forty;
Ploeh.Katas.PropertyBased.Tennis+Score+Advantage;
Ploeh.Katas.PropertyBased.Tennis+Score+Game|]

通过查看最后一个 . 左侧的字符串,您可以找到正在使用的命名空间 - 在本例中为 Ploeh.Katas.PropertyBased .

但是,您应该知道命名空间可以跨越多个程序集。例如, System.Collections.Generic.List<'T> mscorlib 中定义, 而 System.Collections.Generic.Stack<'T> System 中定义.因此,使用上述反射只会为您提供在该特定程序集中的命名空间中定义的成员。

据我所知,F# 模块被编译成带有 [<CompilationMapping(SourceConstructFlags.Module)>] 的静态类。属性。这意味着您可以像这样列出模块:
> open Microsoft.FSharp.Core;;
> let modules =
ts
|> Array.filter
(fun t -> t.GetCustomAttributes<CompilationMappingAttribute>()
|> Seq.exists (fun attr -> attr.SourceConstructFlags = SourceConstructFlags.Module));;

val modules : System.Type [] =
[|Ploeh.Katas.PropertyBased.TennisProperties;
Ploeh.Katas.PropertyBased.Tennis|]

如果要列出 Tennis 中的所有函数模块,你可以这样做:
> let tm = modules |> Array.find (fun t -> t.Name = "Tennis");;

val tm : System.Type = Ploeh.Katas.PropertyBased.Tennis

> let functions = tm.GetMethods ();;

val functions : MethodInfo [] =
[|Player other(Player);
Microsoft.FSharp.Core.FSharpOption`1[Ploeh.Katas.PropertyBased.Tennis+Point] incrementPoint(Point);
Point pointFor(Player, PointsData);
PointsData pointTo(Player, Point, PointsData);
Score scorePoints(Player, PointsData); Score scoreForty(Player, FortyData);
Score scoreDeuce(Player); Score scoreAdvantage(Player, Player);
Score scoreGame(Player); Score score(Score, Player); Score get_newGame();
Score scoreSeq(System.Collections.Generic.IEnumerable`1[Ploeh.Katas.PropertyBased.Tennis+Player]);
System.String pointToString(Point);
System.String scoreToString(System.String, System.String, Score);
System.String ToString(); Boolean Equals(System.Object);
Int32 GetHashCode(); System.Type GetType()|]

您可能想要过滤掉一些继承的方法,例如 ToStringGetHashCode .

关于f# - 你能在 F# 中列出命名空间或模块的内容吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30767311/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com