I've been trying this for about 45 minutes now, but I can't seem to figure out how to get a list of all of the INamedTypeSymbol available in a given compilation. I tried digging through the Compilation.GetTypeByMetadataName stuff, but couldn't figure it out.
I tried using Compilation.GlobalNamespace.GetTypeMembers() and it seems to give me all the IModuleSymbols that are referenced by the current project. (I see mscorlib, System.Core etc.) I then tried getting the type members inside these modules, but they were either empty or contained PrivateImplementation stuff.
Is there an API to get access to all the INamedTypeSymbols available in a compilation?
You need to recurse through all of the namespaces in the global namespace
Yep - I've found the easiest way to do the recursion is with a SymbolVisitor:
// Main code
CSharpCompilation compilation = CSharpCompilation.Create(...);
GetAllSymbolsVisitor visitor = new GetAllSymbolsVisitor();
public class GetAllSymbolsVisitor : SymbolVisitor
public override void VisitNamespace(INamespaceSymbol symbol)
Parallel.ForEach(symbol.GetMembers(), s => s.Accept(this));
public override void VisitNamedType(INamedTypeSymbol symbol)
// Do what you need to here (add to collection, etc.)
@daveaglick You can syntax-highlight that code by wrapping it in
See also http://stackoverflow.com/a/32937808/34397
@SLaks Holy cow - I had no idea I could force language highlighting. One of those simple little things I never picked up...thanks!
Also, your visitor won't catch nested types.
Yeah, just left that out - there's actually a pretty full example here that does:
Thanks guys, that should be more than enough to get me started. :)
@daveaglick that link's broken. I believe it is now:
@drewnoakes Indeed it is! A casualty of refactoring, thanks for updating it.