private static MappingInformation GetInformation(string source) { var syntaxTree = CSharpSyntaxTree.ParseText(source); var references = AppDomain.CurrentDomain.GetAssemblies() .Where(_ => !_.IsDynamic && !string.IsNullOrWhiteSpace(_.Location)) .Select(_ => MetadataReference.CreateFromFile(_.Location)) .Concat(new[] { MetadataReference.CreateFromFile(typeof(MapGenerator).Assembly.Location) }); var compilation = CSharpCompilation.Create("generator", new SyntaxTree[] { syntaxTree }, references, new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary)); var model = compilation.GetSemanticModel(syntaxTree); var receiver = new MapReceiver(); foreach (var attributeSyntax in syntaxTree.GetRoot().DescendantNodes(_ => true).OfType <AttributeSyntax>()) { var context = GeneratorSyntaxContextFactory.Create(attributeSyntax, model); receiver.OnVisitSyntaxNode(context); } foreach (var typeDeclarationSyntax in syntaxTree.GetRoot().DescendantNodes(_ => true).OfType <TypeDeclarationSyntax>()) { var context = GeneratorSyntaxContextFactory.Create(typeDeclarationSyntax, model); receiver.OnVisitSyntaxNode(context); } return(new(receiver, compilation)); }
private static GeneratorSyntaxContext GetContext(SyntaxNode node) { var tree = node.SyntaxTree; var references = AppDomain.CurrentDomain.GetAssemblies() .Where(_ => !_.IsDynamic && !string.IsNullOrWhiteSpace(_.Location)) .Select(_ => MetadataReference.CreateFromFile(_.Location)) .Concat(new[] { MetadataReference.CreateFromFile(typeof(MapGenerator).Assembly.Location) }); var compilation = CSharpCompilation.Create("generator", new SyntaxTree[] { tree }, references, new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary)); var model = compilation.GetSemanticModel(tree); return(GeneratorSyntaxContextFactory.Create(node, model)); }