This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class NamedTypeVisitor : SymbolVisitor | |
{ | |
public override void VisitNamespace(INamespaceSymbol symbol) | |
{ | |
Console.WriteLine(symbol); | |
foreach(var childSymbol in symbol.GetMembers()) | |
{ | |
//We must implement the visitor pattern ourselves and | |
//accept the child symbols in order to visit their children | |
childSymbol.Accept(this); | |
} | |
} | |
public override void VisitNamedType(INamedTypeSymbol symbol) | |
{ | |
Console.WriteLine(symbol); | |
foreach (var childSymbol in symbol.GetTypeMembers()) | |
{ | |
//Once againt we must accept the children to visit | |
//all of their children | |
childSymbol.Accept(this); | |
} | |
} | |
} | |
//Now we need to use our visitor | |
var tree = CSharpSyntaxTree.ParseText(@" | |
class MyClass | |
{ | |
class Nested | |
{ | |
} | |
void M() | |
{ | |
} | |
}"); | |
var mscorlib = MetadataReference.CreateFromFile(typeof(object).Assembly.Location); | |
var compilation = CSharpCompilation.Create("MyCompilation", | |
syntaxTrees: new[] { tree }, references: new[] { mscorlib }); | |
var visitor = new NamedTypeVisitor(); | |
visitor.Visit(compilation.GlobalNamespace); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment