New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Question] How do I get all the INamedTypeSymbols in a given compilation? #6138

Closed
JoshVarty opened this Issue Oct 19, 2015 · 10 comments

Comments

Projects
None yet
5 participants
@JoshVarty
Contributor

JoshVarty commented Oct 19, 2015

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?

@SLaks

This comment has been minimized.

Show comment
Hide comment
@SLaks

SLaks Oct 19, 2015

Contributor

You need to recurse through all of the namespaces in the global namespace

Contributor

SLaks commented Oct 19, 2015

You need to recurse through all of the namespaces in the global namespace

@daveaglick

This comment has been minimized.

Show comment
Hide comment
@daveaglick

daveaglick Oct 19, 2015

Contributor

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();
visitor.Visit(compilation.Assembly.GlobalNamespace);

// Visitor
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.)
    }
}
Contributor

daveaglick commented Oct 19, 2015

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();
visitor.Visit(compilation.Assembly.GlobalNamespace);

// Visitor
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.)
    }
}
@SLaks

This comment has been minimized.

Show comment
Hide comment
@SLaks

SLaks Oct 19, 2015

Contributor

@daveaglick You can syntax-highlight that code by wrapping it in

```C#
Contributor

SLaks commented Oct 19, 2015

@daveaglick You can syntax-highlight that code by wrapping it in

```C#
@SLaks

This comment has been minimized.

Show comment
Hide comment
@SLaks
Contributor

SLaks commented Oct 19, 2015

@daveaglick

This comment has been minimized.

Show comment
Hide comment
@daveaglick

daveaglick Oct 19, 2015

Contributor

@SLaks Holy cow - I had no idea I could force language highlighting. One of those simple little things I never picked up...thanks!

Contributor

daveaglick commented Oct 19, 2015

@SLaks Holy cow - I had no idea I could force language highlighting. One of those simple little things I never picked up...thanks!

@SLaks

This comment has been minimized.

Show comment
Hide comment
@SLaks

SLaks Oct 19, 2015

Contributor

Also, your visitor won't catch nested types.

Contributor

SLaks commented Oct 19, 2015

Also, your visitor won't catch nested types.

@daveaglick

This comment has been minimized.

Show comment
Hide comment
@daveaglick

daveaglick Oct 19, 2015

Contributor

Yeah, just left that out - there's actually a pretty full example here that does:
https://github.com/Wyamio/Wyam/blob/develop/Wyam.Modules.CodeAnalysis/AnalyzeSymbolVisitor.cs

Contributor

daveaglick commented Oct 19, 2015

Yeah, just left that out - there's actually a pretty full example here that does:
https://github.com/Wyamio/Wyam/blob/develop/Wyam.Modules.CodeAnalysis/AnalyzeSymbolVisitor.cs

@JoshVarty

This comment has been minimized.

Show comment
Hide comment
@JoshVarty

JoshVarty Oct 19, 2015

Contributor

Thanks guys, that should be more than enough to get me started. :)

Contributor

JoshVarty commented Oct 19, 2015

Thanks guys, that should be more than enough to get me started. :)

@drewnoakes

This comment has been minimized.

Show comment
Hide comment
Member

drewnoakes commented Sep 19, 2016

@daveaglick

This comment has been minimized.

Show comment
Hide comment
@daveaglick

daveaglick Sep 19, 2016

Contributor

@drewnoakes Indeed it is! A casualty of refactoring, thanks for updating it.

Contributor

daveaglick commented Sep 19, 2016

@drewnoakes Indeed it is! A casualty of refactoring, thanks for updating it.

@iSazonov iSazonov referenced this issue Mar 13, 2018

Merged

Enhance and refactor Add-Type cmdlet #6141

6 of 9 tasks complete
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment