❔ Source code generator with reference to the main project
I have some methods in a project that I want to go through using reflection at compile time to generate some code for them. It seems like source code generators need to be in their own project(?), so I made a project for them in the same solution. The main project uses the generators project. Now, need to access the methods from the main project, but if I reference the main project from the generators project, I would get a circular dependency! Any idea how I would do this?
13 Replies
You can't
Why do you need to use methods from your main project in your generator? What are you actually trying to do?
Well, currently I have a bunch of methods that I find and call using reflection
during runtime
What do those methods do?
They basically make up the standard library for an interpreted language
So I'm trying to generate some source code to do a bit more AOT
So why do you need to call them at source-generation time?
Oh I was a bit unclear, I want to find and analyse them at source-generation
and make it easy to then call them at runtime
Ok, so you don't need to call them
And there's no circular dependency
hm
So how would I get information about the methods in a source generator
By analyzing them
Your source generator needs to register for the appropriate events, and then it will get called back to look at specific things in the project using the generator
Your generator can then inspect things, flow information through, and finally produce new source
That new source gets added to the project using the generator
If you haven't seen it yet, this is the incremental generators API summary: https://github.com/dotnet/roslyn/blob/main/docs/features/incremental-generators.md
Aha I see
I'll have a look at that, thanks!
And remember, #roslyn exists for generator all your roslyn-related questions, including generators 🙂
Ooh nice!
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.