Access SyntaxTree from another project in solution inside a SourceGenerator #50874
-
Hello, I have a problem I can't seem to solve at the moment with source generators, but maybe I'm just missing something. Let's say I have project named "SourceGenerator" which has the source generator class. Than I also have a project called "Model" which references the "SourceGenerator" project. There is also third project called "Repository". What I want to achieve is to use the source generator class to generate automatic repository classes basing it on the models in the "Model" project, but not include them in the "Model" project, but in the "Repository" project. Is it possible to achieve that? Another way that would be acceptable would be to add reference to "SourceGenerator" project in the "Repository" project instead of "Model" project, but then how do I access the the SyntaxTree of "Model" project in the source generator class. The only solution I found is to read all the source files from another project from disk, change them into a compilation object and then analyze syntax trees. It works, but it doesn't use the benefit of syntax visitor as for every change in the code it loads all the files, analyzes them and generates source code. It doesn't seem right. Any ideas how to solve this issue? Thanks in advance. :) |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
So this is probably the right approach, but yes the source generator running against the Repository project can't see the syntax trees of the Model project because it's already been built and compiled. What it can see though is symbols: if the Repository project has a reference to the Model project the types in the Model project will be visible through the symbol APIs. You could then look at classes/methods that way, as long as you don't need to look at the code inside those methods. |
Beta Was this translation helpful? Give feedback.
So this is probably the right approach, but yes the source generator running against the Repository project can't see the syntax trees of the Model project because it's already been built and compiled. What it can see though is symbols: if the Repository project has a reference to the Model project the types in the Model project will be visible through the symbol APIs. You could then look at classes/methods that way, as long as you don't need to loo…