Created
May 22, 2012 22:13
-
-
Save jelster/2771993 to your computer and use it in GitHub Desktop.
sample implementation of a recursive (indirectly speaking) SymbolVisitor for Roslyn CTP. Returns flattened list of type symbols regardless of namespace nesting
This file contains hidden or 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 Walk : SymbolVisitor<NamespaceSymbol, IEnumerable<NamedTypeSymbol>> | |
{ | |
protected override IEnumerable<NamedTypeSymbol> VisitNamespace(NamespaceSymbol symbol, NamespaceSymbol argument) | |
{ | |
var types = new List<NamedTypeSymbol>(); | |
foreach (var ns in symbol.GetNamespaceMembers()) | |
{ | |
var closeTypes = types; | |
closeTypes.AddRange(Visit(ns)); | |
closeTypes.AddRange(ns.GetTypeMembers()); | |
} | |
return types; | |
} | |
} | |
/* and a quick self-standing unit test (xUnit) */ | |
public class TypeSymbolWalkerFixture | |
{ | |
Walk sut = new Walk(); | |
[Fact] | |
public void when_walk_nested_namespaces_with_types_returns_all_unnested_classes() | |
{ | |
var code = @" | |
namespace Bar { | |
public class FooA {} | |
public class BarB {} | |
namespace BarB { | |
public class FooB { public class NestedFoo {} } | |
public class FooC {} | |
} | |
}"; | |
var tree = SyntaxTree.ParseCompilationUnit(code); | |
var comp = Compilation.Create("testA.dll").AddSyntaxTrees(tree); | |
var result = sut.Visit(comp.GlobalNamespace); | |
Assert.NotEmpty(result); | |
Assert.True(result.Count() == 4); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
added tweaked code + a test to prove functionality