Enumerate all types in an assembly inside a macro

179 Views Asked by At

How to get all types in the assembly inside a macro attribute of kind MacroTargets.Assembly in Nemerle?

2

There are 2 best solutions below

0
On

In addition to the answer of Don Reba...

You should use Node.EnsureCached() method to ensure metadata cached.

0
On

The name tree contains all of the types. You can traverse and filter it yourself or you can call its GetTypeBuilders method.

[ Nemerle.MacroUsage
    ( Nemerle.MacroPhase.WithTypedMembers
    , Nemerle.MacroTargets.Assembly
    )
]
macro ListTypes()
{
    def PrintNameTree(node, depth)
    {
        repeat (depth)
            Write("    ");
        Write("|");
        WriteLine(node.PartName);
        unless (node.Children == null)
        {
            foreach (child in node.Children.Values)
                PrintNameTree(child, depth + 1);
        }
    }

    def env = ImplicitCTX().Env;
    def names = env.NameTree;
    PrintNameTree(names.NamespaceTree, 0);
}