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

时间:2015-06-10 21:06:59

标签: f#

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

我正在寻找一种列举价值观和方法的程序化方式,而不是依赖于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
   """

1 个答案:

答案 0 :(得分:8)

据我所知,没有这样的功能可以做到这一点(我只看了FSharpReflectionExtensions模块),但你可以自己写一个。所有的构件都在那里。

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

但是,给定程序集,您可以列出其所有类型或其所有公共类型。这是后者的一个例子,给出了我最近写的做网球卡塔的F#集会:

> 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中定义。因此,使用上面的Reflection只会为您提供在该特定程序集中的命名空间中定义的成员。

据我所知,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