private (ClassReader, CodeModelBuilder) GetClassReaderFromCode( string csharpCode, string className, Func <INamedTypeSymbol, INamedTypeSymbol> walkSymbols = null, Action <List <IPhasedTypeReader> > setupAllReaders = null) { var compilation = TestWorkspaceFactory.CompileCodeOrThrow( csharpCode, references: new[] { this.GetType().Assembly.Location }); var originSymbol = compilation.GetTypeByMetadataName(className); var typeSymbol = walkSymbols?.Invoke(originSymbol) ?? originSymbol; typeSymbol.ShouldNotBeNull($"Type symbol '{className}' could not be found in compilation."); var modelBuilder = new CodeModelBuilder(compilation); var allReaders = new List <IPhasedTypeReader>(); var discoveryVisitor = new TypeDiscoverySymbolVisitor(modelBuilder, allReaders); var readerContext = new CodeReaderContext(modelBuilder.GetCodeModel(), null, LanguageInfo.Entries.CSharp()); compilation.GlobalNamespace.Accept(discoveryVisitor); setupAllReaders?.Invoke(allReaders); var reader = allReaders.OfType <ClassReader>().FirstOrDefault(r => r.TypeSymbol.Equals(typeSymbol)); reader.ShouldNotBeNull($"ClassReader for '{className}' was not registered."); allReaders.ForEach(r => r.RegisterProxy()); return(reader, modelBuilder); }
private IEnumerable <IPhasedTypeReader> VisitCode(string csharpCode) { var compilation = CompileCode(csharpCode); var results = new List <IPhasedTypeReader>(); var visitor = new TypeDiscoverySymbolVisitor(new CodeModelBuilder(compilation), results); compilation.GlobalNamespace.Accept(visitor); return(results); }
public void HowToUse() { // arrange var compilation = CompileCode(@" namespace NS1 { class C1 { } } "); var modelBuilder = new CodeModelBuilder(compilation); var discoveredTypeReaders = new List <IPhasedTypeReader>(); // act var visitor = new TypeDiscoverySymbolVisitor(modelBuilder, discoveredTypeReaders); compilation.GlobalNamespace.Accept(visitor); // assert var discoveredTypeNames = discoveredTypeReaders.Select(r => r.TypeSymbol.Name); }