C
C#ā€¢2y ago
bakk

ā” 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
333fred
333fredā€¢2y ago
You can't Why do you need to use methods from your main project in your generator? What are you actually trying to do?
bakk
bakkā€¢2y ago
Well, currently I have a bunch of methods that I find and call using reflection during runtime
333fred
333fredā€¢2y ago
What do those methods do?
bakk
bakkā€¢2y ago
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
333fred
333fredā€¢2y ago
So why do you need to call them at source-generation time?
bakk
bakkā€¢2y ago
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
333fred
333fredā€¢2y ago
Ok, so you don't need to call them And there's no circular dependency
bakk
bakkā€¢2y ago
hm So how would I get information about the methods in a source generator
333fred
333fredā€¢2y ago
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
bakk
bakkā€¢2y ago
Aha I see I'll have a look at that, thanks!
333fred
333fredā€¢2y ago
And remember, #roslyn exists for generator all your roslyn-related questions, including generators šŸ™‚
bakk
bakkā€¢2y ago
Ooh nice!
Accord
Accordā€¢2y ago
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.