Do you think that only C# version 9.0 onwards supports code generators? .NET Framework projects be darned, Visual Basic the usual disgraced son?
Well, think again.
Code generators are a compiler feature, not a language feature. As long as you build your project with the .NET SDK v6.0 and/or Visual Studio 2022, you can benefit from incremental generators, for example.
(Of course you need to use Roslyn as a compiler; therefore, F# projects are not invited to the party - sorry guys, but hey, your language is so cool you don't even need this kind of stuff, right? ๐)
Don't believe me, either: see for yourself.
- Clone this project.
- Open
CodeGenerationTest.sln
in Visual Studio 2022 (v17.2+ recommended) and build theCodeGenerators
project. - Exit Visual Studio, then open the solution again. This step is needed because VS caches code generators and will not recognize the newly-compiled
CodeGenerators.dll
until you restart it. - Open
Program.cs
in theCSharpApp
project. Verify that there are no errors: both theHello
class and itsWorld
property are present in the assembly, despite this project targeting .NET Framework 4.6.1 (thus, by default, using C# 7.3). - Open
Program.vb
in theVisualBasicApp
project. Verify that there are no errors: both theHello
module and itsWorld
property are present in the assembly, despite this project being in Visual Basic (and, for good measure, explicitly specifying version 15.0 of the language).
On a computer with .NET SDK v6.0 or later installed:
- Clone this project.
- Open the operating system shell of your choice in the folder where you cloned this project.
- Enter the
dotnet build
command. Verify that there are no errors.